2

I have plain JDBC code which is doing transaction management using Connection Interface. I wanted to switch to Spring Transaction Management in small steps.

Firstly I want to provide PlatformTransactionManager for my datasource and annotate my class / methods with @Transaction and keep my other logic same ie. using connection / PreparedStatement etc.

All the examples, which I see use JdbcTemplate. I was wondering can Spring Transaction be used without JdbcTemplate?

Kumar
  • 1,536
  • 2
  • 23
  • 33
  • Why? Just issue SQL transaction statements. – K.Nicholas Oct 10 '21 at 16:58
  • @K.Nicholas: My question is I still don't want to change my code which looks something like: `try (PreparedStatement pstmt = ....createPreparedStatement(connection); ResultSet result = pstmt.executeQuery()) ` I just want the transaction handling to move to Spring Transaction using @Transaction annotation, so that I don't have to take care of explicilty rolling back or commiting transaction etc. – Kumar Oct 10 '21 at 17:05
  • Comes with JDBC doesn't it. – K.Nicholas Oct 10 '21 at 17:09
  • @K.Nicholas: Pure JDBC is too verbose. I want to eventually use Spring Transaction / JPA etc...I want to take small steps like first changing the transaction management with Spring transaction then may be to use jdbcTemplate followed by JPA etc. – Kumar Oct 10 '21 at 17:15
  • 1
    don't bother, just wasting time. – K.Nicholas Oct 10 '21 at 17:16
  • @K.Nicholas: Hmm...But is it possible to use Spring Tx with plain JDBC without JdbcTemplate? – Kumar Oct 10 '21 at 17:20
  • Why? Just issue SQL transaction statements. – K.Nicholas Oct 10 '21 at 17:23
  • @K.Nicholas: I appreciate your advice to stick with SQL statements but for my understanding I wanted to know, if it is possible or not technically? – Kumar Oct 10 '21 at 17:25

2 Answers2

3

Technically it is possible to use @Transactional without JdbcTemplate . But if you try to do it , you will sooner or later find that you are re-inventing what the things that are already done by JdbcTemplate.

What @Transactional does is that before executing a @Transactional method , it will help you to get a JDBC Connection from the DataSource , and start a transaction on this Connection .The JDBC Connection will then stored in a ThreadLocal.

That means if you do it without JdbcTemplate , you have to manually get this Connection from that ThreadLocal such that you can create a JDBC Statement from it for executing your SQL. Not to mention you have to manually release the JDBC resources such Statement , ResultSet etc. properly by yourself which all of these things are already take care by JdbcTemplate.

But if you have already implemented these JDBC codes manually and just want to let @Transactional to handle the transaction , you could try to inject the DataSource to your bean and then use the following method to get the Connection for your JDBC codes use :

    Connection connection = DataSourceUtils.getConnection(dataSource);

Also checkout JdbcTemplate#execute(ConnectionCallback<T> action) , it is useful for migrating the existing JDBC codes that expects a JDBC Connection to work on to JdbcTemplate.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Thanks! I had one more question.... Can I wrap my datasource in TransactionAwareDataSourceProxy instrad of getting connection from DataSourceUtils? – Kumar Oct 18 '21 at 09:36
  • 1
    should be okay as `TransactionAwareDataSourceProxy` as it it also in the type of `DataSource`. – Ken Chan Oct 18 '21 at 18:22
2

Yes it's possible. Adding a @Transactional annotation to your methods, as long as they follow the correct procedure should make your methods transactional. But as others have mentioned if you're in the process of updating your app you might as well ditch plain JDBC and move across to Spring JPA/JDBC (depending on which version of Spring you're using).

mohammedkhan
  • 953
  • 6
  • 14