spring 编程式事务 (spring事务一)

版权声明:随意转载。 https://blog.csdn.net/dengjili/article/details/83933022

配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	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/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>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>

测试类

public class RunTestTx {

// 编程式事务
private static final Logger logger = LoggerFactory.getLogger(TestMapperScanner.class);
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-tx.xml");
		// 事务控制
		DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) context.getBean("transactionManager");
		// 数据库操作句柄
		JdbcTemplate jdbcTemplate = (org.springframework.jdbc.core.JdbcTemplate) context.getBean("jdbcTemplate");
		
		// 开始事务
		TransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus transaction = transactionManager.getTransaction(def);
		
		try {
			String sql = "select * from role where name like concat('%', ?, '%')";
			Object[] objs = {"111"};
			List<Role> resultList = jdbcTemplate.query(sql, objs, (rs, rownum) -> {
				Role role = new Role();
				role.setId(rs.getInt("id"));
				role.setName(rs.getString("name"));
				role.setDesc(rs.getString("desc"));
				return role;
			});
			
			if (logger.isDebugEnabled()) {
				logger.debug(resultList.toString());
			}
			
			transactionManager.commit(transaction);
		} catch (Exception ex) {
			transactionManager.rollback(transaction);
		}
	}
}

测试结果

Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
Acquired Connection [jdbc:mysql://127.0.0.1:3306/test, UserName=root@localhost, MySQL Connector/J] for JDBC transaction
Switching JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, UserName=root@localhost, MySQL Connector/J] to manual commit
Executing prepared SQL query
Executing prepared SQL statement [select * from role where name like concat('%', ?, '%')]
[priv.dengjl.springmybatis.bean.Role@36c88a32, priv.dengjl.springmybatis.bean.Role@7880cdf3, priv.dengjl.springmybatis.bean.Role@5be6e01c, priv.dengjl.springmybatis.bean.Role@1c93084c, priv.dengjl.springmybatis.bean.Role@6ef888f6, priv.dengjl.springmybatis.bean.Role@10e92f8f, priv.dengjl.springmybatis.bean.Role@7ce3cb8e, priv.dengjl.springmybatis.bean.Role@78b66d36, priv.dengjl.springmybatis.bean.Role@5223e5ee, priv.dengjl.springmybatis.bean.Role@bef2d72, priv.dengjl.springmybatis.bean.Role@69b2283a, priv.dengjl.springmybatis.bean.Role@22a637e7, priv.dengjl.springmybatis.bean.Role@6fe7aac8, priv.dengjl.springmybatis.bean.Role@1d119efb, priv.dengjl.springmybatis.bean.Role@659a969b, priv.dengjl.springmybatis.bean.Role@76908cc0, priv.dengjl.springmybatis.bean.Role@2473d930, priv.dengjl.springmybatis.bean.Role@35047d03, priv.dengjl.springmybatis.bean.Role@49b0b76, priv.dengjl.springmybatis.bean.Role@769f71a9, priv.dengjl.springmybatis.bean.Role@4c9f8c13, priv.dengjl.springmybatis.bean.Role@5ae50ce6, priv.dengjl.springmybatis.bean.Role@6f96c77, priv.dengjl.springmybatis.bean.Role@be64738, priv.dengjl.springmybatis.bean.Role@3ba9ad43, priv.dengjl.springmybatis.bean.Role@49d904ec, priv.dengjl.springmybatis.bean.Role@48e4374, priv.dengjl.springmybatis.bean.Role@3d680b5a, priv.dengjl.springmybatis.bean.Role@4b5d6a01, priv.dengjl.springmybatis.bean.Role@4a22f9e2, priv.dengjl.springmybatis.bean.Role@3c419631, priv.dengjl.springmybatis.bean.Role@418e7838, priv.dengjl.springmybatis.bean.Role@61230f6a, priv.dengjl.springmybatis.bean.Role@3c130745, priv.dengjl.springmybatis.bean.Role@cd3fee8, priv.dengjl.springmybatis.bean.Role@3e2e18f2, priv.dengjl.springmybatis.bean.Role@470f1802, priv.dengjl.springmybatis.bean.Role@63021689]
Initiating transaction commit
Committing JDBC transaction on Connection [jdbc:mysql://127.0.0.1:3306/test, UserName=root@localhost, MySQL Connector/J]
Releasing JDBC Connection [jdbc:mysql://127.0.0.1:3306/test, UserName=root@localhost, MySQL Connector/J] after transaction
Returning JDBC Connection to DataSource

猜你喜欢

转载自blog.csdn.net/dengjili/article/details/83933022