多平台统一管理软件接口,如何实现多平台统一管理软件接口
330
2022-12-16
Spring实战之使用TransactionProxyFactoryBean实现声明式事务操作示例
本文实例讲述了Spring实战之使用TransactionProxyFactoryBean实现声明式事务操作。分享给大家供大家参考,具体如下:
一 配置文件
xmlns="http://springframework.org/schema/beans" xmlns:p="http://springframework.org/schema/p" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd"> 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"/> class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> p:ds-ref="dataSource"/> "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao">
xmlns="http://springframework.org/schema/beans"
xmlns:p="http://springframework.org/schema/p"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-4.0.xsd">
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"/> class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> p:ds-ref="dataSource"/> "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao">
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"/>
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> p:ds-ref="dataSource"/> "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
p:ds-ref="dataSource"/> "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao">
p:ds-ref="dataSource"/>
"org.springframework.transaction.interceptor.TransactionProxyFactoryBean" p:transactionManager-ref="transactionManager" p:target-ref="newsDao">
"org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
p:transactionManager-ref="transactionManager"
p:target-ref="newsDao">
二 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&nbsgKZzgp; org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransagKZzgctionInterceptor.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
更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~