提问者:小点点

多线程中的Spring事务管理?


Spring Transaction不支持多线程,所以我尝试在thread的run()方法中手动管理事务。但是,没用!

我想在下面的示例中回滚每个线程的run()方法,当其中有异常抛出时。(在以下情况下,插入到UNKNOWN_TABLE)

我的预期结果是“开始,1,3,5,结束”。

而实际结果是‘开始,1,2,3,4,5,结束’。

欢迎任何回复!谢谢!

主要类别:

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private TestService testService;

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

    @Bean
    public DriverManagerDataSource createDriverManagerDataSource() {

        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setJdbcUrl("jdbc:oracle:thin:@url:port/schema");
        dataSource.setUsername("xxxx");
        dataSource.setPassword("xxxx");

        return dataSource;

    }

    @Bean
    public JdbcTemplate createJdbcTemplate() {

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(createDriverManagerDataSource());

        return jdbcTemplate;

    }

    @Override
    public void run(String... args) throws Exception {

        testService.test();

    }

}

服务等级:

@Service
public class TestService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void test() throws Exception {

        jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('start', 'start')");

        ExecutorService executorService = Executors.newFixedThreadPool(5);

        for (int i = 1; i <= 5; i++) {

            executorService.submit(new TestRunner(i));

        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

        jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('end', 'end')");
    }

    private class TestRunner implements Runnable {

        private Integer id;

        public TestRunner(Integer id) {

            this.id = id;

        }

        @Override
        public void run() {

            try (Connection connection = jdbcTemplate.getDataSource().getConnection()) {

                try {

                    connection.setAutoCommit(false);

                    String sqlString = String.format("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('%d', '%d')", id, id);
                    jdbcTemplate.batchUpdate(sqlString);

                    if (id % 2 == 0) {
                        // Except the transaction been rollback when this.id is 2 or 4.
                        jdbcTemplate.batchUpdate("INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES ('no', 'no')");

                    }

                    connection.commit();

                } catch (Exception e) {

                    System.err.println("Failure: UNKNOWN_TABLE");
                    connection.rollback();

                } finally {

                    connection.close();

                }

            } catch (SQLException e2) {

                e2.printStackTrace();

            }

        }

    }

}

共2个答案

匿名用户

你的代码有几件事,因为你试图超越Spring和Spring Boot。与其试图这样做,不如使用框架而不是围绕它们。

  1. 抛弃你的@Configuration类,让Spring Boot进行配置
  2. 使用事务模板,而不是自己搞砸(错误!)连接
  3. 使用默认配置的Spring TaskExecutor,而不是手动访问执行器

将其添加到<code>应用程序中。属性

spring.datasource.url=jdbc:oracle:thin:@url:port/schema
spring.datasource.username=xxxx
spring.datasource.password=xxxx

使用TransactionTemboard代替乱搞连接。

@@SpringBootApplication
public class Application {

    private static final String SQL = "INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES (?, ?)";
    private static final String ERROR_SQL = "INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES (?, ?)";


    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

    @Bean
    public CommandLineRunner testRunner(JdbcTemplate jdbc, TransactionTemplate tx, TaskExecutor tasks) {
        return (args) -> {
            jdbc.update(SQL, "start", "start");
            IntStream.range(1, 6)
                    .forEach(id -> {
                        try {
                            tasks.execute(() -> tx.executeWithoutResult((s) -> {
                                jdbc.update(SQL, id, id);
                                if (id % 2 == 0) {
                                    jdbc.update(ERROR_SQL, "no", "no");
                                }
                            }));
                        } catch (DataAccessException e) {
                            e.printStackTrace();
                        }
                    });
            jdbc.update(SQL, "end", "end");
        };
    }
}

类似上面的内容会产生您想要的结果。请注意,您现在使用框架提供的< code>JdbcTemplate 、< code>TransactionTemplate和< code>TaskExecutor。

匿名用户

在参考@M.Deinum答案后,我已将代码更改为下面,它满足了我的需求。

application.properties

spring.datasource.url=jdbc:oracle:thin:@ip:port/schema
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

主要类

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private TestService testService;

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

    @Override
    public void run(String... args) throws Exception {

        testService.test();
        System.exit(0);

    }

}

测试服务

@Service
public class TestService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Transactional(rollbackFor = Exception.class)
    public void test() throws Exception {

        jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('start', 'start')");

        ExecutorService executorService = Executors.newFixedThreadPool(5);

        for (int i = 1; i <= 5; i++) {

            executorService.submit(new TestRunner(i));

        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

        jdbcTemplate.batchUpdate("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('end', 'end')");
    }

    private class TestRunner implements Runnable {

        private Integer id;

        public TestRunner(Integer id) {

            this.id = id;

        }

        @Override
        public void run() {

            TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

            transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {

                    String sqlString = String.format("INSERT INTO TB_MYTEST(MYKEY, MYVALUE) VALUES ('%d', '%d')", id, id);
                    jdbcTemplate.batchUpdate(sqlString);

                    if (id % 2 == 0) {

                        jdbcTemplate.batchUpdate("INSERT INTO UNKNOWN_TABLE(MYKEY, MYVALUE) VALUES ('no', 'no')");

                    }

                }

            });

        }

    }

}

结果是“开始,1,3,5,结束”。