• Post Reply Bookmark Topic Watch Topic
  • New Topic

SessionEJB & Transaction...

 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there
I'm a bit confused with the transactions in the EJB context...
To sort things out, i've tried a simple example but, to me, it doesn't work the way it should :
A StatelessSessionEJBBean insert 2 rows in my database.
Each insert is a simple "Insert into.." with a primary key.
The connection.AutoCommit is set to "false" not to disturb the EJB container. The deployement descriptor says that my EJB's <trans-attribute> is "Required".
First time i run my EJB, every thing is ok : 2 rows are created and the commit is well performed.
Before I run the EJB a second time, I remove one of the previously inserted row using SQL*PLUS, then I commit.
Next, I run the EJB a second time (using the same values as the first time)
Of course, an exception is thrown (unique constraint violated for one of the row)
So, what do i have in my database : 2 rows. The one inserted at the first execution and the other one inserted at the the second execution, just before the exception occurs.
In my mind, the exception in the second run should rollback the whole transaction, so no row should be commited this time...
I'm wrong or what ?
If someone can clarify the situation...
Thanx in advance
[ October 15, 2003: Message edited by: Franck Tranchant ]
 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just in case someone care about this or has the same questions as me, here are the points to achieve my simple example (i'm using WebLogic)
1) Use an JDBC Tx (Transaction-aware) Data Source to access the database through a regular Connection Pool.
2) Right before the SQL statement, set the connection's AutoCommit to false.
3) In the EJBBean class, catch and throw EJBException (not only Exception)
4) Configure the EJB behaviour regarding transactions into the ejb-jar.xml file with the <trans-attribute> tag set to Required or RequiresNew.
Voil´┐Ż.
In this configuration, everything worked as expected...
[ October 16, 2003: Message edited by: Franck Tranchant ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Franck, you shouldn't use setAutoCommit(), commit() and rollback() methods in EJBs (see chapter 17.3.4 of the EJB 2.0 specification). The container manages the transactions automatically and any manual transaction demarcation should not be done.
 
Franck Tranchant
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right Lasse,
I thought the connection's AutoCommit default value was true.
That's why I forced it to false..
But the process still works great without it, so let's forget about the step 2)
Thanx for your remark.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!