Spring Boot + Doma2を使ってトランザクション管理をやろうと思っています。
- Controllerが起動された時に自動でトランザクションを開始
- Controllerから例外がthrowされたらば、ロールバック
- Controllerから例外がthrowされなければ、コミット
- Controllerの中でrollback()を呼び出したらばロールバックし、直後にトランザクションを開始する
という実装を行いたいです。
1~3は実装できました。
この2つの記事を参考にしました。
http://qiita.com/ksby/items/2ea40c5455c31870bd7c
http://qiita.com/nyasba/items/1e22c2401f3849f9071d
でも、どうしても4だけが実装できません。
どうにか実装出来ないでしょうか?
コードはこんな感じです。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" rollback-for="Exception" timeout="3"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcutService" expression="execution(* jp.co.example.controller..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutService"/> </aop:config> </beans>
@ImportResource("classpath:transaction-context.xml") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(LogisticsApplication.class, args); } @Resource private Environment env; @Bean DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName( env.getRequiredProperty("spring.datasource.driverClassName")); dataSource.setUrl(env.getRequiredProperty("spring.datasource.url")); dataSource.setUsername(env.getRequiredProperty("spring.datasource.username")); dataSource.setPassword(env.getRequiredProperty("spring.datasource.password")); return new TransactionAwareDataSourceProxy(dataSource); } @Bean SqlFileRepository sqlFileRepository() { return new NoCacheSqlFileRepository(); } @Bean Config domaConfig() { return new Config() { @Override public Dialect getDialect() { return new MysqlDialect(); } @Override public DataSource getDataSource() { return dataSource(); } @Override public SqlFileRepository getSqlFileRepository(){ return sqlFileRepository(); } }; } }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。