###質問内容
現在、Springを用いたwebアプリケーションを開発しております。表題の通りmyBatis-Springで設定したメソッドがSpringのトランザクションとして管理されておりません。そのため、メソッド実行中に例外が発生してもロールバックされずにコミットされてしまいます。公式の設定例等を参考にしているのですがどうしても解決できず、投稿させていただきました。
詳細ですが、以下のログを確認しておりSqlSessionが有効になっておらずJDBCコネクションがspringトランザクションとして管理されていないものと思われます。
2017-5-07 15:24:05.297 DEBUG MYBATIS org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 2017-5-07 15:24:05.309 DEBUG MYBATIS org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26b354be] was not registered for synchronization because synchronization is not active Sun May 07 15:24:05 JST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 2017-5-07 15:24:05.758 DEBUG MYBATIS org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@51b53ec5] will not be managed by Spring
トランザクションの実装方針ですが、今回myBatis-generatorで自動生成したマッパーをアノテーションでなくroot-context.xmlを用いて管理しようとしています。
###root-context.xml
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/> <!-- <context:component-scan base-package="org.ymSample.app" /> --> <!-- Root Context: defines shared resources visible to all other web components --> <mybatis:scan base-package="org.ymSample.dao.mapper"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="connection.autoCommit" value="false" /> </bean> <!-- com.mysql.jdbc.Driver ⇒ mysql-connector-java-5.1.41-bin.jarをWEB-INFのlib配下に置く必要がある --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- generatorで自動生成したマッパーxml --> <property name="mapperLocations" value="classpath*:org/ymSample/dao/sqlxml/*.xml" /> </bean> <!-- jdbcデータソースに対するトランザクションマネージャクラスを設定 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- トランザクション定義情報 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*Get*" read-only="true"/> <tx:method name="*Update*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" rollback-for="Exception" timeout="3"/> <tx:method name="Insert" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" rollback-for="Exception" timeout="3"/> <tx:method name="*Delete*" propagation="REQUIRED" isolation="READ_COMMITTED" read-only="false" rollback-for="Exception" timeout="3"/> </tx:attributes> </tx:advice> <!-- トランザクション定義を適用させる範囲 --> <aop:config> <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* org.ymSample.app.sample.*Service.*(..))" /> </aop:config> <!-- jacksonメッセージコンバーターのBean --> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="indentOutput" value="true"/> </bean> </property> </bean> <!-- メッセージコンバータに、上記のJacksonコンバーターbeanを使用する --> <mvc:annotation-driven> <mvc:message-converters> <ref bean="mappingJackson2HttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> <!-- アプリ内で使用されるメッセージ管理クラス --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:/META-INF/spring/messages"/> </bean> </beans>
上記のログは、以下のクラスのメソッドを実行した際のものになります。
###トランザクション対象としたいメソッド(※Insert)
@Service public class SampleService { private static final Logger logger = LoggerFactory.getLogger(SampleService.class); @Autowired private ArtistsMasterMapper artistsMasterMapper; /** * サービスクラス * @throws Exception */ public void Insert(ArtistsMaster requestDto) throws Exception { logger.info("artistInsertService", "START"); int insertRow = artistsMasterMapper.insert(requestDto); if(insertRow > 0) { System.out.println("実行"); throw new Exception(); } } }
###調査の経過について
デバッグした結果、SqlSessionUtilsクラスのregisterSessionHolderメソッド内の以下の条件分岐でfalseになることが原因であることが分かりました。コメント文からの推測ですが「現在のスレッドに対してトランザクション同期がアクティブでない」という状態が問題のようです。
↓
if (TransactionSynchronizationManager.isSynchronizationActive()) { ~~~ }
###開発環境について
java 1.8
MySQL 5.7
spring 4.3.8.RELEASE
mybatis 3.4.0
mybatis-spring 1.3.0
spring-jdbc 4.3.8.RELEASE
※AOPライブラリ
aspectjrt 1.8.9
spring-aspects 4.3.8.RELEASE



あなたの回答
tips
プレビュー