0

I'm using SQLServer along with Springs JtaTransactionManager.

Given a method like this:

@Transactional
public void something() {
    try (final Connection conn1 = getConnection()) {
        //insert/update stuff in database
    }


    try (final Connection conn2 = getConnection()) {
        //insert/update stuff in database
    }
}

Will this method actually be transactional? What happens to the changes made when conn1 gets closed, are they commited to the database? What happens if an exception occurs while working with conn2, can the changes made through a closed connection be rolled back?

Tobb
  • 11,850
  • 6
  • 52
  • 77
  • 2
    I think you are misusing `@Transactional`, you shouldn't create connection on your own inside it – Ori Marko Dec 19 '19 at 11:09
  • What driver are you using? As the javadoc states [here](https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#close()) these things are `implementation-defined`. – second Dec 19 '19 at 11:10
  • Needed to double-check, turns out it's not transactional, but @TransactionAttribute. So it might be JavaEE, but the project uses a mix of JavaEE and Spring so a bit hard to know. It's LEGACY code (so legacy that it needs to be written in all caps.) – Tobb Dec 19 '19 at 11:44
  • This question is an attempt to generalize this one: https://stackoverflow.com/questions/59376413/transactionmanagement-with-glassfish-spring-xa-and-jta – Tobb Dec 19 '19 at 12:33

1 Answers1

0

You shouldn't load/get connection or datasource manually inside @Transactional, especially when using JtaTransactionManager

The JtaTransactionManager does not need to know about the DataSource, or any other specific resources, because it uses the container’s global transaction management infrastructure.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I'm not sure how that quote relates to my question. I'm looking to figure out what happens when you are in a transaction, and connections are being closed. – Tobb Dec 19 '19 at 12:32
  • @Tobb you shouldn't update connection inside `@Transactional`, all method should be using same connection – Ori Marko Dec 19 '19 at 12:46
  • I'm aware that I shoudn't. The guys who made the code I'm working on, not so much. Which is why I'm trying to figure out how this actually works. – Tobb Dec 19 '19 at 12:53
  • @Tobb if you don't explicit call commit on connection and auto commit is set to false so it won't commit transaction inside method – Ori Marko Dec 19 '19 at 13:51
  • Ok, there is no explicit commit as far as I can see. Not sure about auto-commit, but since changes are in fact made to the database I assume it is on. – Tobb Dec 19 '19 at 13:52
  • The connection is provided by the transactionmanager. As soon as it is created it takes part in the XA-Transaction it handles. So the Transactionmanager handles the complete lifecycle of Database-Connections, at least as long as you use XA-Resources or JTA-Resources, They register with the transactionmanager. – aschoerk Dec 22 '19 at 18:05