Spring数据源事务管理器不回滚

发布于 2024-10-21 00:13:31 字数 1686 浏览 6 评论 0原文

<!-- Data source definition -->
<bean id="dataSourceWebsube"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url.websube}"
    p:username="${jdbc.username.websube}"   p:password="${jdbc.password.websube}" />

<bean id="jdbcTemplateWebsube" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>         

<!-- Transaction manager, actually this one is useless since the Transaction Manager Bean is already called transactionManager -->      
<tx:annotation-driven transaction-manager="transactionManager"/>  

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>

public class Test 
{
@Transactional
public static void testTranscational(JdbcTemplate jdbcTmpl)
{
    String sql = null;                              
    sql = "INSERT INTO NBSM.INT_RTLNOUTBOUND_PRODUCTS(BASEPRODUCTCODE)VALUES(1)";                
    jdbcTmpl.update(sql);           
    throw new RuntimeException();                       
}

public static void main(String[] args) {            
    ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
    JdbcTemplate jdbcTmpl = (JdbcTemplate) ctx.getBean("jdbcTemplateWebsube");      
    testTranscational(jdbcTmpl);
}
}

你好,

我正在尝试测试 spring 的事务管理功能,但我无法让它工作。我已经尝试解决这个问题几个小时了,但没有成功。

上面你可以看到相关的配置xml定义和dao测试类。即使当我使用调试级别进行日志记录时,也没有回滚机制的足迹。

任何帮助将不胜感激

<!-- Data source definition -->
<bean id="dataSourceWebsube"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url.websube}"
    p:username="${jdbc.username.websube}"   p:password="${jdbc.password.websube}" />

<bean id="jdbcTemplateWebsube" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>         

<!-- Transaction manager, actually this one is useless since the Transaction Manager Bean is already called transactionManager -->      
<tx:annotation-driven transaction-manager="transactionManager"/>  

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>

public class Test 
{
@Transactional
public static void testTranscational(JdbcTemplate jdbcTmpl)
{
    String sql = null;                              
    sql = "INSERT INTO NBSM.INT_RTLNOUTBOUND_PRODUCTS(BASEPRODUCTCODE)VALUES(1)";                
    jdbcTmpl.update(sql);           
    throw new RuntimeException();                       
}

public static void main(String[] args) {            
    ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
    JdbcTemplate jdbcTmpl = (JdbcTemplate) ctx.getBean("jdbcTemplateWebsube");      
    testTranscational(jdbcTmpl);
}
}

Hello,

I'm trying to test spring's transaction management capabilities but I cannot get it to work. I have been trying to solve it for hours but no luck.

Above you can see the related config xml definitions and the dao test class. Even when I use debug level for logging, there's no footprint of the rolling back mechanism.

Any help would be greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-10-28 00:13:31

这是因为 Spring AOP 只能:

  • a)在 Spring 管理的 Bean 上工作
  • b)在非静态方法上工作
  • c)仅在调用 Spring AOP 代理时才考虑(您需要从其他 bean 调用该 bean,但是不是来自自身)

a)和b)是你的错误,你需要修复它。 -- 为了克服 c) 的问题,我强烈用 AspectJ 编译时挥手替换 Spring AOP。

如果我是对的,即使是事务处理(由@Transational)也一定无法正常工作。

需要由其他人检查:

我从来没有看到 @TransactionalJdbcTemplate 结合使用,所以我怀疑这是否有效 - 但我可能错了。

It is because of Spring AOP can only:

  • a) work on Spring managed Bean
  • b) work on none static methods
  • c) is only taken in account if the Spring AOP Proxy is invoked (you need to invoke the bean from an other bean, but not from itself)

a) and b) are your faults, you need to fix it. -- to overcome the problem of c) I strongly replace Spring AOP by AspectJ compile time waving.

If I am right, even the transaction handling (by @Transational must not be working.

Need to be checked by someone else:

I have never seen @Transactional in combination with JdbcTemplate, so I have doubt if this is working at all. -- But may I am wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文