Spring’s practical example of using transactionproxyfactorybean to implement declarative transaction operations
•
Java
This article describes how to use transactionproxyfactorybean to implement declarative transaction operations in spring. Share with you for your reference, as follows:
One configuration file
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.MysqL.jdbc.Driver" p:jdbcUrl="jdbc:MysqL://localhost/spring" p:user="root" p:password="32147" p:maxPoolSize="40" p:minPoolSize="2" p:initialPoolSize="2" p:maxIdleTime="30"/> <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager 类 --> <!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现--> <!-- 配置DataSourceTransactionManager时需要依注入DataSource的引用 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 配置一个业务逻辑Bean --> <bean id="newsDao" class="org.crazyit.app.dao.impl.NewsDaoImpl" p:ds-ref="dataSource"/> <!-- 为业务逻辑Bean配置事务代理 transactionManager用于为配置事务代理注入所需的事务管理器 target用于指定为哪个Bean配置事务代理 --> <bean id="newsDaoTrans" class= "org.springframework.transaction.interceptor.TransactionProxyfactorybean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao"> <!-- 指定事务属性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_required</prop> </props> </property> </bean> </beans>
Second Dao
1 interface
package org.crazyit.app.dao; public interface NewsDao { public void insert(String title,String content); }
2 implementation class
package org.crazyit.app.dao.impl; import javax.sql.DataSource; import java.sql.Connection; import org.springframework.jdbc.core.JdbcTemplate; import org.crazyit.app.dao.*; public class NewsDaoImpl implements NewsDao { private DataSource ds; public void setDs(DataSource ds) { this.ds = ds; } public void insert(String title,String content) { JdbcTemplate jt = new JdbcTemplate(ds); jt.update("insert into news_inf" + " values(null,?,?)",title,content); // 两次插入的数据违反唯一键约束 jt.update("insert into news_inf" + " values(null,content); // 如果没有事务控制,则第一条记录可以被插入 // 如果增加事务控制,将发现第一条记录也插不进去。 } }
Three test categories
package lee; import org.springframework.context.support.*; import org.springframework.context.*; import org.crazyit.app.dao.*; public class SpringTest { public static void main(String[] args) { // 创建spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 获取事务代理Bean NewsDao dao = (NewsDao)ctx .getBean("newsDaoTrans",NewsDao.class); // 执行插入操作 dao.insert("疯狂Java","轻量级Java EE企业应用实战"); } }
Four tests
No data in the database indicates that the transaction is effective.
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码