我用的是spring封装的jdbc,用的是jdbcTemplate去处理批量的sql,想用事务机制,就是当一个sql运行出错误的时候,
前面执行的sql都回滚。    例如: String sql="insert into tab1(id,name,age) values(1,'tom',20)";
          String sql2="insert into tab2(groupid,groupname,userid) values(101,'work',11)";
          String sql3="update dep set numb=numb+1 where id=11";
          jdbcTemplate.execute(sql);
          jdbcTemplate.execute(sql2);
          jdbcTemplate.execute(sql3);  如果当执行有问题的时候,把前面执行的都回滚,请问,怎么运用事务机制。我想批量执行sql,这个怎么弄的。请大家帮忙,谢谢。

解决方案 »

  1.   

    spring可以用aop配置事务的,上网查看方法吧,呵呵
      

  2.   


    <bean id="transactionProxyTemplate" abstract="true" lazy-init="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="create*">PROPAGATION_REQUIRED</prop>
    <prop key="submit*">PROPAGATION_REQUIRED</prop>
    <prop key="withdraw*">PROPAGATION_REQUIRED</prop>
    <prop key="distribute*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>spring  的事物
      

  3.   

    这样批量操作就可以实现回滚,我测试了一下,怎么在第三个sql出错的时候,没有实现回滚,请高手指教,我是刚刚使用这个spring
      

  4.   

    <?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: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-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url"
    value="jdbc:postgresql://192.168.2.4:5432/csp" />
    <property name="username" value="postgres" />
    <property name="password" value="password" />
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byName" />
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire="byName"/>
    <tx:advice id="txAdvice">
    <tx:attributes>
          <tx:method name="update*" propagation="REQUIRED"
    rollback-for="Exception" />
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:advisor
    pointcut="execution(public * org.ht.dao.*.*(..))"
    advice-ref="txAdvice" />
    </aop:config>红色的配置说明
    http://220.130.0.97/javaweek/javaweekly.php把你的那三个操作放到一个方法里,方法名开头为update,然后把他放到org.dao包下就可以了