-
1. Re: Open Session in View in EJB3
bill.burke Sep 19, 2005 1:14 PM (in response to surak)"surak" wrote:
I'm trying to get my head around how to implement the open-session-in-view
pattern in EJB3.
Normally this is done by having an interceptor and letting this close and
commit the session resp. transaction at the end of the http-request.
Are you opening/closing up a JTA transaction in the Servlet Filter? If so, then you don't have to do anything as the JTA transaction will be propagated to the EJB that is interacting with the EntityManager. If not, then you have to do your own plumbing and can use an EXTENDED persistence context:
EntityManagerFactory.createEntityManager(EXTENDED)
will give you an EntityManager who's persistence context (and managed objects) will last beyond the life of the JTA transaction. If you don't want to manage the lifecycle you can use a Stateful Session Bean to do this:
@Stateful
public class MyConverationBean ... {
@PersistenceContext(type=EXTENDED)
private EntityManager manager;
}
You get me? or maybe I'm not offering a good solution. -
2. Re: Open Session in View in EJB3
gavin.king Sep 19, 2005 1:35 PM (in response to surak)To use open session in view you will need a client demarcated transaction. It simply can't be done using EJB declarative transaction management.
This is one of the problems that Seam is designed to solve:
http://www.jboss.com/products/seam -
3. Re: Open Session in View in EJB3
surak Sep 20, 2005 2:42 PM (in response to surak)"bill.burke@jboss.com" wrote:
"surak" wrote:
I'm trying to get my head around how to implement the open-session-in-view
pattern in EJB3.
Normally this is done by having an interceptor and letting this close and
commit the session resp. transaction at the end of the http-request.
Are you opening/closing up a JTA transaction in the Servlet Filter? If so, then you don't have to do anything as the JTA transaction will be propagated to the EJB that is interacting with the EntityManager. If not, then you have to do your own plumbing and can use an EXTENDED persistence context:
EntityManagerFactory.createEntityManager(EXTENDED)
will give you an EntityManager who's persistence context (and managed objects) will last beyond the life of the JTA transaction. If you don't want to manage the lifecycle you can use a Stateful Session Bean to do this:
@Stateful
public class MyConverationBean ... {
@PersistenceContext(type=EXTENDED)
private EntityManager manager;
}
You get me? or maybe I'm not offering a good solution.
Should it be enough to begin a JTA-transaction in the filter? Just to try it out I wrote a filter that looks up the EntityManagerFactory from jndi and creates a new EntityManager from which I do getTransaction upon which I call begin().
But even after doing this I get a LazyException when trying to get a lazy collection, so it seems like the session is still being closed after the bean-method is executed.
I looked at the Seam-project, as suggested by Gavin, which seems to be really great, but unfortunally is it a bit late in our project to switch to JSF now. -
4. Re: Open Session in View in EJB3
bill.burke Sep 20, 2005 2:47 PM (in response to surak)THis should work. I will try to reproduce when I get a chance. In the meantime, could you post a jira task with a sample app that uses the filter to create a JTA transaction and reproduces your problem? It would make things go faster on my end.
-
5. Re: Open Session in View in EJB3
epbernard Sep 21, 2005 5:50 AM (in response to surak)em.getTransaction().begon() does not start a JTA tx, but a regular resource Tx.
-
6. Re: Open Session in View in EJB3
surak Sep 21, 2005 7:09 AM (in response to surak)"bill.burke@jboss.com" wrote:
THis should work. I will try to reproduce when I get a chance. In the meantime, could you post a jira task with a sample app that uses the filter to create a JTA transaction and reproduces your problem? It would make things go faster on my end.
I just created a sample-app and attached this to the following JIRA-task.
http://jira.jboss.com/jira/browse/EJBTHREE-299
The affected code is included and displays the failure.
Thanks for your help!
Johan -
7. Re: Open Session in View in EJB3
surak Sep 21, 2005 10:17 AM (in response to surak)"epbernard" wrote:
em.getTransaction().begon() does not start a JTA tx, but a regular resource Tx.
Thanks very much for pointing this out! I'm now starting a real JTA-tx and the lazy loading is now working a tiny bit better ;)
Cheers
Johan -
8. Re: Open Session in View in EJB3
grzegorz.knapczyk Oct 20, 2005 2:59 AM (in response to surak)Could you please explain how to start JTA-tx? I tried with the following code:
EntityManagerFactory factory = (EntityManagerFactory) portal.ActionServlet.ctxLookup("java:/EntityManagerFactories/manager1"); EntityManager manager = factory.createEntityManager(PersistenceContextType.EXTENDED); EntityTransaction transaction = manager.getTransaction(); transaction.begin(); // here is the part where I try to use some LAZY fetched properties, but the exception is thrown transaction.commit();
The exception is:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: PrRecord.modifications, no session or session was closed
-
9. Re: Open Session in View in EJB3
bill.burke Oct 20, 2005 11:15 AM (in response to surak)javax.transaction.UserTransaction ut = (UserTransaction)initialContext.lookup("java:/UserTransaction");
-
10. Re: Open Session in View in EJB3
grzegorz.knapczyk Oct 21, 2005 4:33 AM (in response to surak)Thanks for your quick reply. I have tried with javax.transaction.UserTransaction, but it still doesn't work. Server throws the exception LazyInitializationException.
javax.transaction.UserTransaction ut = (UserTransaction)initialContext.lookup("java:comp/UserTransaction"); ut.begin(); (...) // here I try to use LAZY propererty, but the exeption is thrown ut.commit();
10:25:21,950 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: org.aplusc.tvman.ejb.entity.PrRecord.modifications, no session or session was closed
-
11. Re: Open Session in View in EJB3
franconian_dude May 18, 2006 4:08 PM (in response to surak)I got the same problem an spinning my head around but can't find any proper or running solution ... my code is
struts actionInitialContext initialContext = ServiceLocator.getInitialContext(); usertransaction = (UserTransaction)initialContext.lookup("UserTransaction"); usertransaction.begin(); IEventService eventService = ServiceFactory.getEventService(); Event event = eventService.findEventById(44l); System.out.println(event.getEventCompetitors().size()); usertransaction.commit();
-
12. Re: Open Session in View in EJB3
franconian_dude May 18, 2006 4:11 PM (in response to surak)session bean
@TransactionAttribute(TransactionAttributeType.MANDATORY) public Event findEventById(long id) throws EJBException { return entityManager.find(Event.class, id); }
Does anyone know why this is not running properly, of course with the FAMOUS LazyInitializationException ... any help would be gr8 ... thx in advance -
13. Re: Open Session in View in EJB3
epbernard May 18, 2006 4:53 PM (in response to surak)if you can wrap that in a minimal reproducable test case, please post a JIRA issue
-
14. Re: Open Session in View in EJB3
marcel_costa_almeida Dec 28, 2006 7:37 AM (in response to surak)I have a important doubt on this issue.
If I understood correctly from the Jira Issue (EJBTHREE-299), we can only use the Entity Manager in a Open Session in View style if the EJB are @Local, is it?
Isn´t there any other option for the @Remote beans?