温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring使用XML方式管理声明式事务操作

发布时间:2021-06-04 16:19:24 来源:亿速云 阅读:100 作者:Leah 栏目:编程语言

Spring使用XML方式管理声明式事务操作?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一 配置文件

<?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"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-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,指定事务管理器 -->
   <tx:advice id="txAdvice"
      transaction-manager="transactionManager">
      <!-- 用于配置详细的事务语义 -->
      <tx:attributes>
        <!-- 所有以'get'开头的方法是read-only的 -->
        <tx:method name="get*" read-only="true" />
        <!-- 其他方法使用默认的事务设置,指定超时时长为5秒 -->
        <tx:method name="*" isolation="DEFAULT"
           propagation="REQUIRED" timeout="5" />
      </tx:attributes>
   </tx:advice>
   <!-- AOP配置的元素 -->
   <aop:config>
      <!-- 配置一个切入点,匹配org.crazyit.app.dao.impl包下 所有以Impl结尾的类里、所有方法的执行 -->
      <aop:pointcut id="myPointcut"
        expression="execution(* org.crazyit.app.dao.impl.*Impl.*(..))" />
      <!-- 指定在myPointcut切入点应用txAdvice事务增强处理 -->
      <aop:advisor advice-ref="txAdvice"
        pointcut-ref="myPointcut" />
   </aop:config>
</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("newsDao" , 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.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
     at  org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
     at  org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
     at com.sun.proxy.$Proxy5.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)

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI