Showing posts with label ejb. Show all posts
Showing posts with label ejb. Show all posts
Monday, 8 March 2010
Transaction Pitfalls
Just thought that I need to post this very useful article about common transaction mistakes in Java platform. It's just so easy to assume that transaction works and then in the end (or worse, in production), you find out that it does not work as intended. The classic "Transaction Rollback" mistake is surprisingly common. I've seen this again and again as people assume that this would work automatically.
Tuesday, 19 February 2008
Accessing EJB 3 from Spring 2.5
Spring 2.5 now supports EJB 3 access, and it could not be any easier! Here is the spring xml snippet to achieve it:
<beans>
...
<!-- *** EJBs *** -->
<!-- REMOTE JNDI CONNECTION -->
<bean id="remoteJndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">
localhost
</prop>
<prop key="java.naming.security.principal">
username
</prop>
<prop key="java.naming.security.credentials">
mycredentials
</prop>
</props>
</property>
</bean>
<bean id="abstractRemoteSlsb" abstract="true"
class="org.springframework.ejb.access.
SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="lookupHomeOnStartup" value="false">
<property name="jndiTemplate">
<ref bean="remoteJndiTemplate" />
</property>
</bean>
<bean id="myService" parent="abstractRemoteSlsb">
<property name="jndiName"
value="myapp/MySessionBeanImpl/remote">
<property name="businessInterface"
value="au.com.package.name.MySessionBean">
</bean>
...
</beans>
Wednesday, 13 February 2008
EJB 3.0 LazyInitializationException In Unit Tests
I have to say that unit testing in EJB is very challenging. All I want is doing unit tests on a local session bean that someone else created, so here is what I did:
- Create a remote interface
- Create a base interface from the current local session bean interface
- Delete all abstract methods on the local interface and extend the base interface
- Update the remote interface to extend the base interface
- Add the methods that I want to unit test in the remote interface
- Change the methods that I want to test in the implementation to public
- Edit the implementation class so that it extends both the local and remote interface
Gosh, if that is not time consuming, tell me what is! After all the effort, finally I wrote the actual unit test class, wrote the code to get the remote home that I created earlier using JNDI in the setUp() method, wrote my test, compile the whole project, and then start my JBoss.To my surprise, I got LazyInitializationException, so I thought, all the effort that I did earlier was useless! I tried googling and found some ways to avoid this exception. There are actually 4 ways to do this:
- Change the session bean to be stateful, and change the persistence context type to EXTENDED. Do I really need to do this just to get the test working?
- Run the session from within another session bean. Do I really need to create another one just to get the test working?
- Make fetch type EAGER (obviously). Impossible! There are lots of data, and therefore the laziness
- Create another method and use Hibernate's JOIN FETCH strategy. This one seems to be the cleanest and the least destructive solution
Honestly, why is it so hard to unit test a Session Bean? If the project had been done in Spring, there are Spring tools that can be utilised and therefore unit testing would have been much easier! See my earlier post to work around this Exception in Spring.
Sunday, 22 April 2007
Enterprise JavaBeans 3.0

As well as covering EJB 3.0 in depth, this book is also a good introduction for Java programmers developing server side programming. I use this book as a reference for the project that I'm currently working on.
Subscribe to:
Posts (Atom)