PDA

View Full Version : Using EJB Transaction Declarations



tnine
May 16th, 2005, 05:05 PM
Hi All,
I'm having an issue that I can't seem to find the answer to. I am trying to get the Hibernate DAO's I have defined to participate in an existing transaction. As a project requirement, we need to define our transactions at the EJB level. We are running clustered Remote Stateless beans on Jboss. I have defined the following below.




<bean id="myTransactionManager"
class="org.springframework.transaction.jta.JtaTransaction Manager">


However, the only place to reference a transactionmanager is within the org.springframework.transaction.interceptor.Transa ctionInterceptor. This is not what we want to do. We want the ejb invocation to invoke a spring getBean, then execute the implementation, leaving the transactional declaration to the EJB container. I cannot for the life of me find any examples of using the JtaTransactionManager to reference a containers transaction managment. Can anyone provide an example of getting the declarative transactions from the EJB container?

wpoitras
May 17th, 2005, 02:19 PM
I believe what you would do is create the transaction manager and everything as if you were doing the transaction yourself. However, when you specify the transaction properties you could specify that you wanted to propagate the transaction from the EJB to your Spring bean. I would suggest one of the following:
- PROPAGATION_MANDATORY - Support a current transaction, throw an exception if none exists.
- PROPAGATION_REQUIRED - Support a current transaction, create a new one if none exists.

See interface TransactionDefinition (http://www.springframework.org/docs/api/org/springframework/transaction/TransactionDefinition.html) for more propapation values.

tnine
May 17th, 2005, 04:04 PM
If I'm following your statement correctly, you are suggesting that I define my transaction in both my ejb-jar.xml file, as well as my applicationcontext xml file? Here is the overall picture of what I am trying to do.

1. We use Hibernate 2 for a majority of DAO code, however, we also use the Spring JDBC Templates to call stored procedures.

2. We want CMT to keep our architecture consistent.

3. Given requirement 1 and 2 we want a transaction to begin on the ejb call such that all Hibernate Sessions, as well as Spring JDBC template calls are within the same transaction.

Therefore, I need to join the existing Container Managed Transaction with all JDBC calls, regardless of their origin from Spring or Hibernate.

wpoitras
May 17th, 2005, 04:19 PM
That's pretty much what I'm saying.

The thing is, calling Spring based code is similar to calling another EJB. The symantics are very similar. If you were going to invoke another EJB from within your outside EJB call, you would have to defined the transaction properties for the inner EJB and use similar propagation properties.

tnine
May 17th, 2005, 05:09 PM
That makes sense, but my superiors would prefer to keep the definition at the ejb-jar.xml level. Is it not possible to simply join a CMT with Spring?

wpoitras
May 18th, 2005, 04:22 PM
You probably could do it programitically using a TransactionTemplate (http://www.springframework.org/docs/api/org/springframework/transaction/support/TransactionTemplate.html)

It would still be the same principle as you'd have to instantiate a transaction manager and specify the propagation properties somewhere. Unfortunately I don't know that much about using programatic transactions. The Spring reference manual is a good place to start, I'm not sure where else you would get good information on it.