zoukankan      html  css  js  c++  java
  • spring + ibatis 整合后,事务的问题

    • ibatis独立事务处理

       String resource="SqlMapConfig.xml";
        Reader reader = null;
        try {
           reader = Resources.getResourceAsReader(resource);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(reader);

        

    •    spring编程事务处理

    transactionTemplate = (TransactionTemplate)factory.getBean("transactionTemplate");
            List<Testtb> tbs = null;
            tbs = transactionTemplate.execute(new TransactionCallback(){
                    public Object doInTransaction(TransactionStatus ts){
                    try
                    {

                    
                     }
                    catch (Exception e)
                    {
                         ts.setRollbackOnly();
                    }
                    finally
                    {
                                       }
                 }
             });

    • spring 声明事务

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean id="propertyConfig" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="location"> 
       <value>jdbc.properties</value> 
      </property> 
    </bean> 
     

    <bean id="dataSource"
      class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
      <value>java:comp/env/jdbc/mysqlDB</value>
      </property>
      </bean>
      <!--
    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
      <property name="driverClassName"> 
       <value>${jdbc.driver}</value> 
      </property> 
      <property name="url"> 
       <value>${jdbc.url}</value> 
      </property> 
      <property name="username"> 
       <value>${jdbc.username}</value> 
      </property> 
      <property name="password"> 
       <value>${jdbc.password}</value>  
      </property> 
    </bean> 
      -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource" /> 
    </bean>

    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <aop:config>
       <aop:pointcut  id="pointcut1" expression="execution(* com.ibc.service.*.*(..))"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
        <tx:method name="InsertSomedata" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
       </tx:attributes>
    </tx:advice>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        <property name="transactionManager" ref="transactionManager" />  
        <property name="transactionAttributes">  
        <props>  
            <prop key="InsertSomedata">PROPAGATION_REQUIRED,-Exception</prop>  
        </props>  
        </property>  
    </bean>

    <!--绿色部分任选一种方法即可--> 

    <!--
    <bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
        <property name="beanNames">
         <list>
             <value>testDatabaseImpl1</value>
         </list>
         </property> 
        <property name="interceptorNames"><value>transactionInterceptor</value></property> 
    </bean> 
    -->

     
    <bean id="sqlMapClient" 
      class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
      <property name="configLocation"> 
       <value>SqlMapConfig.xml</value> 
      </property> 
      <property name="dataSource"> 
       <ref bean="dataSource" /> 
      </property> 
    </bean> 
     

    <bean id="sqlMapClientTemplate" 
      class="org.springframework.orm.ibatis.SqlMapClientTemplate"> 
      <property name="sqlMapClient"> 
       <ref bean="sqlMapClient" /> 
      </property> 
    </bean> 

    <!--

    public class TestDatabaseImpl extends SqlMapClientDaoSupport implements TestDatabase

    {

    }

    (1)这个class必须继承于SqlMapClientDaoSupport , spring 才能管理ibatis的事务。

    (2)另外方法中不能自己try、catch拦截异常。

    上述两点是网上很多帖子询问的“回滚不了”的原因。

    --> 

    <bean id="testDatabaseImpl1"    class="com.ibc.service.TestDatabaseImpl">
    <property name="sqlMapClientTemplate"> 
       <ref bean="sqlMapClientTemplate" /> 
      </property>
    </bean>

    </beans>

  • 相关阅读:
    路由配置系统(views)
    Django-MTV简介
    web框架
    APScheduler
    python单例模式
    mysql_istall_db
    mysql多线程写入出现脏数据(重复数据)问题?
    南方周末 【1999年新年献词】总有一种力量让我们泪流满面
    卡特兰树通向证明
    可持久化线段树(主席树)快速简洁教程 图文并茂 保证学会。kth number例题
  • 原文地址:https://www.cnblogs.com/qizhenglong/p/2857673.html
Copyright © 2011-2022 走看看