How to use Container Managed Transaction (CMT) with JBoss AS 6, Hibernate 3.6, JPA, JTA and EJB3

折月煮酒 提交于 2019-12-05 17:59:25
jpkrohling

1) The answer is in the same document you linked :-)

In the examples above you can see access to the SessionFactory. How do you get access to the factory everywhere in your code? Again, if you run in a Java EE environment, or use an embedded service in JSE, you could simply look it up from JNDI, where Hibernate can bind it on startup. Another solution is to keep it in a global static singleton after startup.

2) The name is a reference to the name specified in the tag persistence-unit from your persistence.xml. In your case, it's myPersistanceUnitName.

3) Yes, if you understand that it's not meant to replace anything, it's meant to facilitate the consumption of "beans" (which may be resources from the AS).

Also note that in order to get a managed EntityManager (with benefits), you should not get the EntityManagerFactory from the AS. You should get the EM itself. Like you mentioned, it's like this:

@PersistenceContext(name="myPersistanceUnitName") EntityManager em;

It will work only in things which are managed by the AS, like Servlets, other CDI beans, EJB, ... Not sure if Struts Action classes would get it injected :-)

traviskds

I also have this issue. I have narrowed it down to when using @WebService and we inject the EntityManager via @PersistenceContext, the entitymanager is null.

Also if we delegate the data access to a DAO and add the @TransactionAttribute(TransactionAttributeType.MANDATORY) for the DAO class, it throws the javax.ejb.EJBTransactionRequiredException.

Wonder if this is a JBoss bug that does not add stateless session beans that have@WebService annotations to a container managed transaction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!