質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

Q&A

0回答

11464閲覧

myBatis-springを使用してもSpringのトランザクション管理が行われない

退会済みユーザー

退会済みユーザー

総合スコア0

Spring

Spring Framework は、Javaプラットフォーム向けのオープンソースアプリケーションフレームワークです。 Java Platform上に、 Web ベースのアプリケーションを設計するための拡張機能が数多く用意されています。

MyBatis

MyBatisはJavaや.NET Frameworkでなどで使用できる、SQL文や、ストアドプロシージャをオブジェクトと紐付けるO/Rマッピングフレームワークです。

1グッド

1クリップ

投稿2017/05/07 07:40

編集2022/01/12 10:55

###質問内容
現在、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

Hiroyuki-Nagata👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

A-pZ

2017/05/09 08:16

設定に誤りはないようにみえますが、transactionAdvice の対象メソッドでどのメソッドを実行したときのログでしょうか。なお、設定中にInsertだけ完全一致になっているのが気になりました。
A-pZ

2017/05/16 11:28

念のため、SampleServiceのパッケージが、<aop:advisor advice-ref="transactionAdvice" pointcut="execution(* org.ymSample.app.sample.*Service.*(..))" />と合っているかも確認してください。気になるのは、ym になっていることです。(mySampleなら英単語してあっているので)
退会済みユーザー

退会済みユーザー

2017/05/23 13:31

たびたびありがとうございます。返信遅くなり、申し訳ありません。ymはアプリケーションの頭文字からとったので誤りではありません。。どうやらTransactionSynchronizationManager.isSynchronizationActive()というメソッドがfalseになることが直接の原因のようです。。
退会済みユーザー

退会済みユーザー

2017/05/23 13:33

メソッドをさらにデバッグしたところコネクションホルダーが設定されていないようでした、、
退会済みユーザー

退会済みユーザー

2017/05/23 13:34

設定される条件などを調査してみます。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問