Transactions in EJB

hi all,

I am little bit confused with EJB Transactions.

What I heard is we cannot control transactions in EJB with Connection.setAutoCommint(false) and Connection.commit() and Connection.rollback();

the EJB Container Starts a Transaction , if everything goes well the transaction will be commited otherwise in the catch block we have use SessionContext.setRollbackOnly();

but I am getting different results...

I have Stateless Session Bean, Transaction is Container in managed in ejb-jar.xml and Transaction attribute required.

I am inserting two records in the same table and same database with the same connection object and when I am using SessionContext.setRollbackOnly(); in the catch block and when the second insert is throwing an exception... now the transaction is not getting rolled back i.e. the first record is being inserted.

and when I am controlling it with Connection.setAutocommit(false) and Connection.commit and Connection.rollback() in the catch block it's working fine.

i.e. when second statement throwing some exception the transaction is getting rolled back and when there is no exception the transaction is getting commited.

Does Transactions are managed in differently in different Containers.

I am using OC4J Server.

[1315 byte] By [Prashant.Naidua] at [2007-10-2 5:13:35]
# 1

Hi,

I'm not exactly sure about your problem, but you might want to check what kind of data source you're using (the object you get your db connection from). The data source (and the db that stands behind it) must be aware of container transaction processing. If the two are not linked properly it could be that the transaction manager is not aware of your data source participating in a transaction. It is natural that transactions based on your db connection (manual connection.commit() and connection.rollback()) work well for local operations. This only becomes an issue when you have many distributed components that participate in a transaction.

I would start solving this problem by looking at your container's (server's) documentation and checking for data source settings and deployment (DataSource vs XADataSource).

I hope this helps.

Mark

markcitizena at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

hi mark,

thx for reply,...

I am sorry, I din't check your reply as I was busy with some other project,....

you are asking me to check the Data Source,... but I am getting the Connection Object in the normal way that how we do it from a stand alone program not from the connection pool,

that is DriverManager.getConnection()

and I am using classes12.jar for jdbc purpose as My database is oracle.

is it that we need to use connection pooling or need to do other setting so that our methods participate in transaction

Prashant.Naidua at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Hello,

I'm not sure but,

my logic tells me that the container using JTA can only roll-bacl transaction of connection defined in a pool.

Furthermore,

It's not recommanded to create a custom connection from an EJB.

The containerr provides these services, us it.

Regards,

Sebastien Degardin.

sdegardina at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
If you have to control the transaction then set the transaction element in ejb-jar.xml to Bean managed and then use the commit and rollback methods.
enigma_y2k1a at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
hi enigma, can't I use connection.rollback and connection.commit without specifing Bean managed in ejb-jar.xml
Prashant.Naidua at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
> can't I use connection.rollback and> nd connection.commit without specifing Bean managed> in ejb-jar.xmlNo, you can't.
annie79a at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
hi anni, but yes it's working, I have user connection.commit() and connection.rollback() and it's working fine, I am using OC4J server
Prashant.Naidua at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
not user connection in previous reply-- used connection
Prashant.Naidua at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

When your are not using Connection.setAutoCommit(false):

-

In this case whenever you are inserting record, that is basically one transaction. Next your are inserting another record. That is another transaction. In your case first transaction is fine. Therefore that is committed automatically. But your second transaction not working properly. There is no link with first transaction. Therefore only second transaction is rolled back.

When your are using Connection.setAutoCommit(false):

--

In this case you are overriding the automatic commit property on the connection object. Therefore two insert operations are working as a single transaction. YES it is the purpose of the Connection.setAutoCommit(false) method call. Therefore two insertions are related. Whenever your second transaction is not working whole bunch is rolled back. It is perfectly fine.

Conclusion:

--

I beleive my above comments are true for any containers.

If I am wrong please communicate me the right answer.

Malika at 2007-7-16 1:16:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...