I really got a lot of problems with the transaction management (with both the declarative and the programmatic way).
I use jboss(2.4)/jetty and the jboss-db hypersonic.
Since the programmatic way didn't work, I decided to first make it declarative... but this also does not work.
I've written a simple servlet, which invokes the method transact() of a (also simple) Bean.
The Bean just updates a single entry in the database and then causes an exception. I expect the database-update not to happen, but when I test the entries, the db-update is done, even when the exception was thrown.
The code of the servlet is trivial. It just invokes transact().
The code of the bean is:
// connect to the database (which actualy works!)
// initialize statement-object
stmt.executeUpdate("UPDATE table1 SET word = 'two' WHERE word='one'");
// now the code that causes a System-Exception
throw new EjbException();
On the jboss-console, I get the following message:
[ERROR,Transactor] TRANSACTION ROLLBACK EXCEPTION:
10; nested exception is:
I've looked at the documentation and found:
RollbackException exception is thrown when the transaction has been marked for rollback only or the transaction has been rolled back instead of committed. This is a local exception thrown by methods in the UserTransaction, Transaction, and TransactionManager interfaces.
So... everything should be correct... but if I get the entry, it is 'two' and not 'one' anymore. I tried a lot of things and didn't forget to change the db-entry back to 'one', everytime I test it.
I've also tested the entries in the ejb-jar.xml. I changed it to "Mandatory" and got the error message, that I have to invoke transact() from within a transaction... so I think it should work with "Required"!
How do you get the database connection?
If you lookup the datasource "java:/DefaultDS"
and ask it for a connection, the connection will
automatically be added to the transaction.
For other methods, you have to add the connection
to the transaction yourself.
String url = "jdbc:hsqldb:hsql://localhost:1476";
conn = DriverManager.getConnection(url, "guest", null);
stmt = conn.createStatement();
should do it correctly, or am I wrong?
After testing a few things and making a few changes, I know got the error message a bit clearer, I think.
The "core"-error-message is:
"Transactor" is the name of my Bean (meanwhile, a Stateful Session Bean).
What are .ser-Files?
Using the DriverManager does not enroll the
connection in the transaction.
Use the datasource bound at "java:/DefaultDS"
The .ser files are passivated session beans.
Your session bean is probably not serializable?
Thanks a lot!
That was it! I now configured the connection in the .jcml and the rollback() is done correct.
Thank you so much!
(and now, I try to increase my contribution to far beyond 3 articles per 18 months :-)