How Propagation.REQUIRES_NEW works on jdbc level?

Viewed 59

I've read the documentation and I understand how Propagation.REQUIRES_NEW works but

Create a new transaction, and suspend the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.
NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available to it (which is server-specific in standard Java EE).
See Also:
org.springframework.transaction.jta.JtaTransactionManager.setTransactionManager

I can't understand how suspension could work.

For a single level transaction I suppose that spring creates the code like this:

Connection connection = DriverManager.getConnection(...);

try {
  connection.setAutoCommit(false);
  PreparedStatement firstStatement = connection.prepareStatement(...);

  firstStatement.executeUpdate();

  PreparedStatement secondStatement = connection.prepareStatement(...);

  secondStatement.executeUpdate();
  connection.commit();
} catch (Exception e) {
  connection.rollback();
}

Could you please provide an example for the Propagation.REQUIRES_NEW?

Is it done somehow via jdbc savepoint?

1 Answers

but I can't understand how suspension could work.

It mostly doesn't.

Is it done somehow via jdbc savepoint ?

JDBC doesn't support the notion of suspending transactions (it supports the notion of subtransactions, though - that's what savepoints are about. JDBC does, that is - many DB engines do not).

So how does it work?

By moving beyond the confines of JDBC. The database needs to support it, and the driver also needs to support it, outside of the JDBC API. So, via a non-JDBC-based DB interaction model, or by sending an SQL command.

For example, In WebLogic, there's the WebLogic TransactionManager. That's not open source, so I have no idea how it works, but the fact that it's a separate API (not JDBC) is rather telling.

It's also telling that the javadoc of JtaTransactionManager says that there are only 2 known implementations, and that these implementations steer quite close to the definitions in JTA.

Straight from that javadoc:

WebSphere-specific PlatformTransactionManager implementation that delegates to a UOWManager instance, obtained from WebSphere's JNDI environment.

So, JNDI then. "Voodoo skip JDBC talk directly to the database magic" indeed.

Related