EJB programming & troubleshooting: EJB Commit() & Rollback() not working
Hi, We hava a J2EE application in WAS 6.0. We are using EJBs which calls a DAO layer. Oracle 10g is the backend and we use Oracle SPs for any CRUD operation. There is a save operation as part of our application which does multiple insert into multiple tables in different methods. There should be a rollback to happen on any exceptions or errors. We are using Session Beans(stateless) where the transactions are Container Managed and we rollback using setRollbackOnly() method of the Session Context. But we dont see any rollback happenning in case of exceptions, even though the setRollbackOnly() is called. Have anyone fixed this kind of error before? Thanks Suresh
- Posted by: Suresh Dhandapani
- Posted on: January 09 2009 10:08 EST
- Re: EJB Commit() & Rollback() not working by ishtiak sk on January 11 2009 02:23 EST
- Re: EJB Commit() & Rollback() not working by Asiri Liyanage on January 11 2009 23:35 EST
- Re: EJB Commit() & Rollback() not working by Ignatius Joseph on January 13 2009 16:00 EST
I think it is the way you have handled JDBC connection. If you are calling SP, then I think you may have to look at possibility on setting autocommit as false, on the same connection used for calling SP. It is just a guess.... Thanks, Ishtiak http://interview-questions.weebly.com
The autocommit is set to false in our case. We are creating connection as Connection conn = getConnection(">"); Connection myConn = (java.sql.Connection) WSJdbcUtil .getNativeConnection((WSJdbcConnection) conn); We do this as we are using oracle types oracle.sql.ARRAY and oracle.sql.ArrayDescriptor in our Stored procs. The connection object without type casting (in the second line above) is not supporting ARRAY and ArrayDescriptor.
If you are using jndi look up to get a database connection, check whether you are using XA connection factory.
Check the transaction attribute specified for those methods. Transaction attributes are specified for container managed transactions in ejb-jar.xml. For further information on transaction attributes please check the link below: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html
The transaction attribute is set to "Required". We identified that the issue was with the WsJdbcConnection that we use as part of the DAO layer. The DAO classes uses oracle.sql.ARRAY and oracle.sql.ArrayDescriptor to pass arrays to the Oracle SPs. The connection is created as below Connection conn = getConnection(>); OracleConnection myConn = (OracleConnection) WSJdbcUtil.getNativeConnection((WSJdbcConnection) conn); In ejb-jar.xml, the transaction attribute of the method submitCustomer is set to “Required”. Note – The rollback and commit are happening if java.sql.Connection is used. But java.sql.Connection cannot be used as it doesn’t accept oracle.sql.ARRAY and oracle.sql.ArrayDescriptor classes
the problem is caused by the line below: OracleConnection myConn = (OracleConnection) WSJdbcUtil.getNativeConnection((WSJdbcConnection) conn); when you are using a native/physical connection instead of the wrapper connection provided by container, you will loss the transactional capability provided by XA datasource. this is because the wrapper connection knows when to enlist into a global transaction but the physical/native connection never knows this kind of information.