spring 註解事務 (spring事務二)
阿新 • • 發佈:2018-11-28
xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- spring與mybatis的整合類 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybaties.xml" /> </bean> <!-- 掃dao包 --> <bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="priv.dengjl.springmybatis.dao"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> </bean> <!-- 掃service包 --> <context:component-scan base-package="priv.dengjl.springmybatis.service" /> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 程式設計式即採用註解的方式,事務掃描開始(開啟註解@Tranctional) --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置資訊 --> <typeAliases> <typeAlias type="priv.dengjl.springmybatis.bean.Role" alias="role"/> </typeAliases> <mappers> <mapper resource="priv/dengjl/springmybatis/mapper/RoleMapper.xml" /> </mappers> </configuration>
事務註解
@Service public class RoleListServiceImpl implements RoleListService { @Autowired private RoleService roleService; @Override @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, timeout=3) public void insertRole(List<Role> roles) { for (Role role : roles) { roleService.insertRole(role); } } }
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper dao;
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED, timeout=3)
public void insertRole(Role role) {
dao.insertRole(role);
}
}
其他重要介面
@Repository
在配置檔案xml中,指明瞭dao層註解為Repository
@Repository
public interface RoleMapper {
void insertRole(Role role);
Role selectRole(int id);
}
測試類
public class TestRun {
private static final Logger logger = LoggerFactory.getLogger(TestRun.class);
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-tx-config.xml");
RoleListService bean = context.getBean(RoleListService.class);
List<Role> roles = new ArrayList<>();
Role role = new Role();
role.setId(10087);
role.setName("10087");
role.setDesc("10087");
roles.add(role);
role = new Role();
role.setId(10088);
role.setName("10088");
role.setDesc("10088");
roles.add(role);
bean.insertRole(roles);
logger.debug(bean.toString());
}
}
測試資訊
Creating new transaction with name [priv.dengjl.springmybatis.service.RoleListServiceImpl.insertRole]: PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED,timeout_3
Acquired Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] for JDBC transaction
Changing isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 2
Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to manual commit
Adding transactional method 'priv.dengjl.springmybatis.service.RoleServiceImpl.insertRole' with attribute: PROPAGATION_REQUIRES_NEW,ISOLATION_READ_COMMITTED,timeout_3
Suspending current transaction, creating new transaction with name [priv.dengjl.springmybatis.service.RoleServiceImpl.insertRole]
Acquired Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] for JDBC transaction
Changing isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 2
Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to manual commit
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlS[email protected]]
JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] will be managed by Spring
==> Preparing: insert into role(id, name, `desc`) values(?, ?, ?)
==> Parameters: 10087(Integer), 10087(String), 10087(String)
<== Updates: 1
Releasing transactional SqlSession [[email protected]]
Transaction synchronization committing SqlSession [[email protected]]
Transaction synchronization deregistering SqlSession [[email protected]]
Transaction synchronization closing SqlSession [[email protected]]
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J]
Resetting isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 4
Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] after transaction
Returning JDBC Connection to DataSource
Resuming suspended transaction after completion of inner transaction
Suspending current transaction, creating new transaction with name [priv.dengjl.springmybatis.service.RoleServiceImpl.insertRole]
Acquired Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] for JDBC transaction
Changing isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 2
Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to manual commit
Creating a new SqlSession
Registering transaction synchronization for SqlSession [[email protected]]
JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] will be managed by Spring
==> Preparing: insert into role(id, name, `desc`) values(?, ?, ?)
==> Parameters: 10088(Integer), 10088(String), 10088(String)
<== Updates: 1
Releasing transactional SqlSession [[email protected]]
Transaction synchronization committing SqlSession [[email protected]]
Transaction synchronization deregistering SqlSession [[email protected]]
Transaction synchronization closing SqlSession [[email protected]]
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J]
Resetting isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 4
Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] after transaction
Returning JDBC Connection to DataSource
Resuming suspended transaction after completion of inner transaction
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J]
Resetting isolation level of JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] to 4
Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, [email protected], MySQL Connector/J] after transaction
Returning JDBC Connection to DataSource
[email protected]