gpt4 book ai didi

java - 事务不回滚

转载 作者:行者123 更新时间:2023-12-02 12:31:14 25 4
gpt4 key购买 nike

我使用 spring 4.3.9.RELEASE

一些配置和代码:

@Configuration
@EnableTransactionManagement
public class PersistenceContext {

@Autowired
private DbConfiguration dbConfiguration;

@Bean(destroyMethod = "close")
public DataSource dataSource() {

final BoneCPDataSource dataSource = new BoneCPDataSource();

dataSource.setDriverClass(dbConfiguration.getDriverClassName());
dataSource.setJdbcUrl(dbConfiguration.getUrl());
dataSource.setUsername(dbConfiguration.getUsername());
dataSource.setPassword(dbConfiguration.getPassword());
dataSource.setIdleConnectionTestPeriodInMinutes(dbConfiguration.getIdleConnectionTestPeriod());
dataSource.setIdleMaxAgeInMinutes(dbConfiguration.getIdleMaxAgeInMinutes());
dataSource.setMaxConnectionsPerPartition(dbConfiguration.getMaxConnectionsPerPartition());
dataSource.setMinConnectionsPerPartition(dbConfiguration.getMinConnectionsPerPartition());
dataSource.setPartitionCount(dbConfiguration.getPartitionCount());
dataSource.setAcquireIncrement(dbConfiguration.getAcquireIncrement());
dataSource.setStatementsCacheSize(dbConfiguration.getStatementsCacheSize());

return dataSource;
}

@Bean(name = "txManager")
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
}

servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:annotation-config/>

<context:component-scan base-package="ru.org.*"/>

<tx:annotation-driven transaction-manager="txManager" />

<mvc:annotation-driven />
</beans>

网络配置:

    @Configuration
@EnableScheduling
@EnableWebMvc
@ComponentScan({"ru.org.*"})
@EnableTransactionManagement
public class WebConfig extends WebMvcConfigurerAdapter {


@Bean
public MainHandler mainHandler() {
return new MainHandler();
}
}

处理程序:

public class MainHandler extends AbstractUrlHandlerMapping {

@Autowired
private DataSource dataSource;

protected Object getHandlerInternal(final HttpServletRequest request) throws Exception {
transactionalTest();
}

@Transactional(transactionManager = "txManager")
private void transactionalTest() {

final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("INSERT INTO marks(score) VALUES (1);");
jdbcTemplate.execute("IN3ERT INTO marks(score) VALUES (1);");
}
}

它抛出异常但没有发生回滚。

我还尝试使用 rollbackFor-param 并抛出该异常。

有什么想法吗?

最佳答案

@Transaction 注解有两个主要规则你必须牢记

  1. 如果带注释的方法未在中声明,@Transaction 将起作用调用它的同一个类(默认 Spring Proxy AOP 为 True)。
  2. 如果在公共(public)方法上进​​行注释,@Transaction 将起作用。它将永远如果该方法是私有(private)的、 protected 或包可见的,则被忽略。

那么你应该做什么

Delcare @Service 注解类中的公共(public) transactionalTest() 方法,然后在 MainHandler 类中调用它

关于java - 事务不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45276095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com