0

In Glassfish, there is a JDBC Pool option called

Non Transactional Connections

So am I correct in thinking that "Non Transactional Connections" is the same as setting auto-commit=false ?

If that is correct, then why, when this option is disabled (i.e. non-transactional enabled) do I get an error saying

org.postgresql.util.PSQLException: Cannot commit when autoCommit is enabled.

This is when I have java code that looks like :

try {
preparedStatement = connection.prepareStatement(.....);
preparedStatement.executeQuery();
connection.commit();
}
Little Code
  • 1,315
  • 2
  • 16
  • 37

1 Answers1

1

Non Transactional Connections does not set the autoCommit property to false by default. That's not what non - transactional connections are for. From the Oracle glassfish documentation below,

The main advantage of using non-transactional connections is that the overhead incurred in enlisting and delisting connections in transaction contexts is avoided. However, use such connections carefully. For example, if a non-transactional connection is used to query the database while a transaction is in progress that modifies the database, the query retrieves the unmodified data in the database. This is because the in-progress transaction hasn’t committed. For another example, if a non-transactional connection modifies the database and a transaction that is running simultaneously rolls back, the changes made by the non-transactional connection are not rolled back.

You should

Connection con = ds.getConnection();
boolean initValue = con.getAutoCommit(); 
con.setAutoCommit(false);
//do your work here and commit or rollback
con.setAutoCommit(initValue );
ramp
  • 1,256
  • 8
  • 14