Spring Datasource事务管理器问题
final DataSource ds = DataSourceLocator.getInstance()
.getDataSource(sg.cmpl.starhub.lprs.Constants.APP_KEY);
final DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(ds);
final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
final TransactionStatus status = txManager.getTransaction(def);
Connection conn = null;
PreparedStatement ps = null;
try {
/***************************************************************************/
conn = DataSourceUtils.getConnection(ds);
ps = conn.prepareStatement(sql);
ps.execute();
/***************************************************************************/
txManager.commit(status);
} catch (Exception e) {
txManager.rollback(status);
}
我的事务管理器逻辑有问题吗? 看起来不稳定。 当我插入新数据时,第一次似乎保存了,后来我在mysql数据库中找不到数据。 请帮忙。 多谢。
final DataSource ds = DataSourceLocator.getInstance()
.getDataSource(sg.cmpl.starhub.lprs.Constants.APP_KEY);
final DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(ds);
final DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
final TransactionStatus status = txManager.getTransaction(def);
Connection conn = null;
PreparedStatement ps = null;
try {
/***************************************************************************/
conn = DataSourceUtils.getConnection(ds);
ps = conn.prepareStatement(sql);
ps.execute();
/***************************************************************************/
txManager.commit(status);
} catch (Exception e) {
txManager.rollback(status);
}
Is there something wrong with my transaction manager logic? It looks like unstable. When I insert new data, First time it seems to save and later I can't find the data in mysql database. Please help. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您出于特殊原因想要进行编程连接和事务管理,我建议您查看 Spring 的
JdbcTemplate
并将其用法包装在TransactionTemplate
中。然而,正如前面的评论所述,您应该尽可能避免编程式事务管理,并使用注释(
@Transactional
)或 XML 配置(TransactionProxyFactoryBean
或
) 来代替。Assuming there's a special reason you want to do programmatic connection and transaction management I suggest taking a look at Spring's
JdbcTemplate
and wrap it's usage in aTransactionTemplate
.However as stated in the previous comment, you should try to avoid programmatic transaction management as much as possible and use annotations (
@Transactional
) or XML configuration (TransactionProxyFactoryBean
or<tx:advice/>
) instead.是的,有问题。 这不是Spring的方式。 您不应该将提交/回滚逻辑放入这样的代码中。 当您可以在配置中以声明方式执行此操作时,优势就来了。
查看 Spring 事务参考文档。
Yes, there is something wrong. This is not the Spring way. You should not be putting commit/rollback logic in code like this. The advantage comes when you can do it declaratively, in configuration.
Have a look at the Spring transaction reference docs.
附带说明:根据 Spring 文档,如果提交操作因 TransactionException 失败,则已经调用回滚,并且在 catch 块中再次调用它将触发 IllegalTransactionStateException。
As a side note: according to Spring documentation if commit operation fails with TransactionException then rollback was already called and calling it again in catch block will trigger IllegalTransactionStateException.