I'm trying to understand why i need use @Transactional in some cases, let's see.
If i use this method without @Transactional annotation the jboss return on log: "Close the connection yourself. Closing connection for you".
public void doSomething() {
((Session) em.getDelegate()).doWork(new Work() {
@Transactional(TransactionPropagationType.NEVER)
public void execute(Connection connection) throws SQLException {
StringBuilder sqlSP = new StringBuilder();
sqlSP.append("{ call ");
sqlSP.append("myprocedure");
sqlSP.append("(?)}");
connection.setReadOnly(true);
CallableStatement cs = connection.prepareCall(sqlSP.toString());
cs.setInt(1, 1020);
cs.execute();
//FORCE A EXCEPTION and JBOSS SHOW "Closing connection for you"
throw new MyException("FATAL ERROR");
}
});
}
But when i put "@Transactional" the problem disappear, i don't understand why. Someone can explain ?
Standard way of handling DB calls is to : 1. open connection 2. open session and/or transaction 3. do stuff 4. close transaction and/or seasion 5. close connection
That's basicly what that annotation do for You ;) I strongly suggest to learn more about dealling with those kind of things.