[英]Spring Transaction (@Transaction) with Plain JDBC without JdbcTemplate
I have plain JDBC code which is doing transaction management using Connection Interface.我有使用连接接口进行事务管理的普通 JDBC 代码。 I wanted to switch to Spring Transaction Management in small steps.
我想小步切换到 Spring 事务管理。
Firstly I want to provide PlatformTransactionManager
for my datasource and annotate my class / methods with @Transaction and keep my other logic same ie.首先,我想为我的数据源提供
PlatformTransactionManager
并使用 @Transaction 注释我的类/方法并保持我的其他逻辑相同,即。 using connection / PreparedStatement etc.使用连接/ PreparedStatement 等。
All the examples, which I see use JdbcTemplate.我看到的所有示例都使用 JdbcTemplate。 I was wondering can Spring Transaction be used without JdbcTemplate?
我想知道可以在没有 JdbcTemplate 的情况下使用 Spring Transaction 吗?
Technically it is possible to use @Transactional
without JdbcTemplate
.从技术上讲,可以在没有
JdbcTemplate
情况下使用@Transactional
。 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
.但是,如果您尝试这样做,您迟早会发现您正在重新发明
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
. @Transactional
所做的是在执行@Transactional
方法之前,它会帮助您从DataSource
获取一个 JDBC Connection
,并在此Connection
上启动一个事务。然后 JDBC Connection
将存储在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.这意味着如果您在没有
JdbcTemplate
情况下执行此操作,则必须从该ThreadLocal
手动获取此Connection
,以便您可以从中创建一个 JDBC Statement
来执行您的 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
.更不用说您必须自己手动正确释放 JDBC 资源,例如
Statement
, ResultSet
等,所有这些都已由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 :但是,如果您已经手动实现了这些 JDBC 代码并且只想让
@Transactional
来处理事务,您可以尝试将DataSource
注入到您的 bean 中,然后使用以下方法获取您的 JDBC 代码使用的Connection
:
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
.还可以查看
JdbcTemplate#execute(ConnectionCallback<T> action)
,它对于将需要 JDBC Connection
工作的现有 JDBC 代码迁移到JdbcTemplate
很有用。
Yes it's possible.是的,这是可能的。 Adding a
@Transactional
annotation to your methods, as long as they follow the correct procedure should make your methods transactional.向您的方法添加
@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).但正如其他人所提到的,如果您正在更新应用程序,您最好放弃普通 JDBC 并迁移到 Spring JPA/JDBC(取决于您使用的 Spring 版本)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.