Hi,
I am trying to use JPA/Hibernate with EJB2.1 and use the CMTs. I wrote a simple method in my Stateless session bean (2.1) to insert an entity say 'Project' in the database.
Code:
public Project insertArifact(Project project) {
EntityManager em = JPAUtils.getEntityManagerFactory().createEntityManager();
em.persist(project);
if(project.getName().equalsIgnoreCase("shivreet")){
throw new NullPointerException();
}
return project;
}
The project that I am trying to insert has the name and thus the EJB would through a RuntimeException rolling back the transaction. But this does not happen. The entity does get inserted. While if i change the method to
Code:
public Project insertArifact(Project project) {
EntityManager em = JPAUtils.getEntityManagerFactory().createEntityManager();
[color=red]em.joinTransaction();[/color]
em.persist(project);
if(project.getName().equalsIgnoreCase("shivreet")){
throw new NullPointerException();
}
return project;
}
i.e explicitly join a transaction,it works fine . The JPAUtils is a singleton that returns an EntityManagerFactory based on the following persistence.xml
Code:
<persistence-unit name="pps-jpa" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.datasource" value="jdbc/XYZ" />
<property name="hibernate.connection.username" value="user" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hibernate.session_factory_name" value="MySessionFactory" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
<property name="jta.UserTransaction" value="java:comp/UserTransaction" />
</properties>
</persistence-unit>
Any help would be appreciated.
If its any help, I noticed while debugging that when the following call heirarchy is invoked, there is a comment in the JoinableCMTTransactionFactory
checkTransactionSyncStatus(SessionImpl) --> registerSynchronizationIfPossible(JDBCContext)-->
isTransactionInProgress(JDBCContext)-->
isTransactionInProgress(JoinableCMTTransactionFactory)
Code:
public boolean isTransactionInProgress(
JDBCContext jdbcContext, Context transactionContext, Transaction transaction
) {
if ( transaction == null ) return false; [size=18]//should not happen though[/size]
JoinableCMTTransaction joinableCMTTransaction = ( (JoinableCMTTransaction) transaction );
joinableCMTTransaction.tryJoiningTransaction();
return joinableCMTTransaction.isTransactionInProgress( jdbcContext, transactionContext );
}
}
And the transaction seems to be null the first time even though I am in the EJB and it returns a false. This is the call heirarchy
[/img]