Springのトランザクション制御
XMLベースの宣言型トランザクション制御(構成方法)
- ステップ1:必要なjarパッケージをプロジェクトのlibディレクトリにコピーする
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
- ステップ2:Spring構成ファイルを作成して制約をインポートする
<?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">
</beans>
- 手順3:構成ファイルでビジネスレイヤーと永続化レイヤーのペアを構成する
<bean id="accountService" class="cn.myp666.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="cn.myp666.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///db1"></property>
<property name="username" value="root"></property>
<property name="password" value="1419"></property>
</bean>
トランザクション構成手順
- 手順1:トランザクションマネージャーを構成する
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
- 手順2:トランザクションマネージャーを参照するようにトランザクション通知を構成する
<tx:advice id="txAdvice" transaction-manager="transactionManager">
</tx:advice>
- ステップ3:トランザクションの属性を構成する
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
- ステップ4:AOPポイントカット式を構成する
<aop:config>
<aop:pointcut expression="execution(* cn.myp666.service.impl.*.*(..))"
id="pt1"/>
</aop:config>
- ステップ5:ポイントカット式とトランザクション通知の間の対応を構成する
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
注釈ベースの構成
- ステップ1:必要なjarパッケージをプロジェクトのlibディレクトリにコピーする
- ステップ2:制約をインポートしてスキャン済みパッケージを構成するためのSpring構成ファイルを作成する
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.myp666"></context:component-scan>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/db1"></property>
<property name="username" value="root"></property>
<property name="password" value="1419"></property>
</bean>
</beans>
注釈の構成手順
- 手順1:トランザクションマネージャーを構成し、データソースを挿入する
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
- ステップ2:ビジネスレイヤーで@Transactionalアノテーションを使用する
@Service("accountService")
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
public void transfer(String sourceName, String targeName, Float money) {
Account source = accountDao.findAccountByName(sourceName);
Account target = accountDao.findAccountByName(targeName);
source.setMoney(source.getMoney()-money);
target.setMoney(target.getMoney()+money);
accountDao.updateAccount(source);
accountDao.updateAccount(target);
}
}
- この注釈の属性は、xmlの属性と同じ意味を持っています。この注釈は、インターフェース、クラス、およびメソッドに表示できます。
インターフェイスに表示され、インターフェイスのすべての実装クラスがトランザクションをサポートしていることを示します。
クラスに表示され、クラス内のすべてのメソッドにトランザクションサポート
があることを示しますメソッドに表示され、メソッドにトランザクションサポートがあることを示します。
上記の3つの位置の優先順位:メソッド>クラス>インターフェース
- 3番目のステップ:構成ファイルでアノテーショントランザクションのSpringサポートを開く
<tx:annotation-driven transaction-manager="transactionManager"/>