2

在下面的代码方法中doService1()更新正确的 sql 但doService2()sql 有一些问题,但是当我调用它时,即使有一个因为 有一个类型,但当我调用它时doService(),它必须将更新提交到 DB,但是当我 nun 时,这个更新不会提交 DB..doService1()doService2()sql exceptiondoService2()REQUIRES_NEW PropagationdoService1()

@Service public class DbClass {

      static Logger log = Logger.getLogger(
              DbClass.class.getName());

@Autowired
private DataSource dataSource;

@Transactional(propagation=Propagation.REQUIRED)
public void doService(){
    doService1();
    doService2();
}

@Transactional(propagation=Propagation.REQUIRED)
public void doService1(){
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "  update BATCHJOBSTATUS set PROCESSINGDATE = '20130322'  " ;
    int rowCount1 =  jdbcTemplate.update(sql);
    System.out.println(" rowCount1 >" + rowCount1);
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2(){
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "  update aa set a_name = 'hhh' where a_id = 4 and " ;
    int rowCount1 =  jdbcTemplate.update(sql);
    System.out.println(" rowCount2 >" + rowCount1);
}   
}

正如你们的建议一样,也可以按照以下方式进行测试,但仍然面临同样的问题。这里我doService2()在一个单独的班级,但即使仍然有与上面相同的问题

@Service
public class DbClass {

  static Logger log = Logger.getLogger(
          DbClass.class.getName());

@Autowired
private DataSource dataSource;

@Autowired
private DbClass2 dbClass2;

@Transactional
public void doService(){
    doService1();
    dbClass2.doService2();
}

@Transactional(propagation=Propagation.REQUIRED )
public void doService1(){
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "  update BATCHJOBSTATUS set PROCESSINGDATE = '20130322'  " ;
    int rowCount1 =  jdbcTemplate.update(sql);
    System.out.println(" rowCount1 >" + rowCount1);

    }


}


@Service
public class DbClass2 {


@Autowired
private DataSource dataSource;

@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2() {
    System.out.println("*******doService2*********`");

    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    String sql = "  update aa set a_name = 'hhh' where a_id_ = 4  " ;

    int rowCount2 =  jdbcTemplate.update(sql);

    System.out.println(" rowCount2 >" + rowCount2);

}

}



<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
         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

        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">


    <context:annotation-config />

    <context:component-scan base-package="com.spring"/>

      <tx:annotation-driven transaction-manager="txManager1" proxy-target-class="true"/>



    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@192.168.8.121:1521:h3" />
        <property name="username" value="admin" />
        <property name="password" value="admin" />
    </bean>


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

    <bean id="batchJob" class="com.spring.jdbc.BatchJob">
    </bean>


</beans>
4

3 回答 3

5

我之前遇到过同样的问题,在这里解决了:@Transactional(propagation=Propagation.REQUIRES_NEW) 的奇怪行为

使用默认设置,当您doService2()从同一类调用时不会创建任何新的事务代理,因此您的注释不是用户。

为避免此问题,您可以放入doService2()另一个类或使用 aspectJ 进行事务,方法是这样声明:<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

最佳解决方案将取决于您的应用。(这里的第二个似乎更合适)

于 2013-04-03T19:43:41.303 回答
4

调用doService2()可能没有运行任何事务建议,因为我假设您使用的是 JDK 动态代理(接口代理)而不是基于 CGLIB 的代理。如果您还不知道它是如何工作的,您可能需要阅读:http ://static.springsource.org/spring/docs/3.0.x/reference/aop.html#aop-proxying 。

如果您不使用 CGLIB(目标类代理),则在您调用时它永远不会通过 Spring Transaction advisor,doService2()因为它直接调用该方法,而不是通过 Spring 在启动时为您的服务创建的包装器。

您可以通过移动doService2() 到不同的服务类然后将其注入此服务来使您的示例正常工作。这样,您将通过代理并运行交易建议。

否则,如果您准备对您的项目进行更大的更改,您可以让您的示例按原样工作:1)确保 CGLIB 在您的类路径中,并且 2)打开proxy-target-class,或强制它使用 CGLIB 代理所以通过摆脱你的服务接口。

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

如果您要这样做,请确保您已阅读第一个链接:)。

于 2013-04-03T19:36:56.537 回答
1

根据spring 文档(检查第 10.5.6.1 节),spring 框架事务将回滚仅RunTimeException
不适用于其他检查的异常,例如SqlException. 所以如果你真的想要回滚这个异常,你必须像下面这样指定它

@Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=SQLException.class)
public void doService2(){
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String sql = "  update aa set a_name = 'hhh' where a_id = 4 and " ;
    int rowCount1 =  jdbcTemplate.update(sql);
    System.out.println(" rowCount2 >" + rowCount1);
}

试试这个,让我知道它是否有效。
也可能有更多帮助。

于 2013-09-02T11:35:11.987 回答