您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Spring中怎么利用TransactionProxyFactoryBean實現聲明式事務操作,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
一 配置文件
<?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>
二 DAO
1 接口
package org.crazyit.app.dao;public interface NewsDao{ public void insert(String title, String content);}
2 實現類
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 , ? , ?)" , title , content); // 如果沒有事務控制,則第一條記錄可以被插入 // 如果增加事務控制,將發現第一條記錄也插不進去。 }}
三 測試類
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企業應用實戰"); }}
四 測試
數據庫中無數據,說明事務生效。
Exception in thread "main" org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [insert into news_inf values(null , ? , ?)]; Duplicate entry '瘋狂Java' for key 'news_title'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '瘋狂Java' for key 'news_title' at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:660) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980) at org.crazyit.app.dao.impl.NewsDaoImpl.insert(NewsDaoImpl.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy4.insert(Unknown Source) at lee.SpringTest.main(SpringTest.java:28)Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '瘋狂Java' for key 'news_title' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.Util.getInstance(Util.java:384) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:147) at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:916) at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:909) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644) ... 18 more
上述就是小編為大家分享的Spring中怎么利用TransactionProxyFactoryBean實現聲明式事務操作了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。