1

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?

Dan
  • 3,647
  • 5
  • 20
  • 26
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

0

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thank you for your answer but I still confused. lets try to answer for direct questions. Is it possible to implement sub trunsaction using jdbc save points ? based on your answer I understood that answer is yes. Does default spring jdbc provider supports it ? I didn't get. Does any existing JDBC provider supports it ? Please don't generalize answer for non JDBC context – gstackoverflow May 27 '22 at 17:40
  • Yeah, subtransactions can be done with savepoints, but what you want (which is _park_ a transaction and go do something else, to pick it up later) is not the same thing. Parking a transaction (freezing it / suspending it) is not something that can be done with savepoints or any other JDBC feature directly as far as I'm aware. It's: Make a `Statement` and execute some DB-specific SQL that does this, or nothing. – rzwitserloot May 27 '22 at 23:35
  • Sorry I stilll a bit confused and I would like to ask direct question: Is it correct that Propagation.REQUIRES_NEW is not supported for jddbc at all ? in your answer I see only reference to the WebSphere-specific PlatformTransactionManage related to JNDI. – gstackoverflow May 31 '22 at 19:58
  • Base JDBC has nothing baked into it that would support this, which explains why spring's docs explicitly call out that at least out of the box this only works for weblogic and websphere. That doesn't mean it's impossible using base JDBC, but the DB has to expose some custom SQL command to do it, and then in JDBC you can `.createStatement()` and execute this custom SQL. – rzwitserloot May 31 '22 at 22:28
  • Sorry, I still can't understand the whole idea of this Propagation.REQUIRES_NEW if it doesn't work for JDBC – gstackoverflow Jun 08 '22 at 08:19
  • Let's say I have a database that has the ability to blast Eine Kleine Nachtmuzik from Mozart out your speakers. JDBC most certainly does not support this - there is no `con.playMusic("Nachtmuzik");` API. However, I can still do it: `Statement s = con.createStatement("MOZART 'Nachtmuzik'"); s.execute();` - but you'd have to know the exact SQL syntax for each and every DB engine. – rzwitserloot Jun 08 '22 at 12:27