2015-12-02 128 views
1

我想在我的项目中使用@Transactional,我在一个方法中有两个DML语句。即使第一个语句之后有异常,我的第一个DML语句也会被优先处理。想要回滚,我该怎么办?我尝试过很多方面,但没有帮助。Spring jdbcTemplate @transactional不会回滚postgres

我applicationContext.xml文件是:

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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:c="http://www.springframework.org/schema/c" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:flow="http://www.springframework.org/schema/webflow-config" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:jms="http://www.springframework.org/schema/jms" 
    xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:osgi="http://www.springframework.org/schema/osgi" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:util="http://www.springframework.org/schema/util" 

    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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
     http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd 
     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd 
     http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 
"> 

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" id="dataSource"> 
    <property name="driverClass" value="org.postgresql.Driver"></property> 
    <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/stats"></property> 
    <property name="user" value="stats"></property> 
    <property name="password" value="stats"></property> 
    <property name="maxPoolSize" value="50" /> 
    <property name="minPoolSize" value="10" /> 
</bean> 

我的服务接口:

public interface AssignFAServicesInterface { 
public int saveAssignedFA(final String[] CVListCode, final int  userCodeFA, final String randomNoColumn, final String[] tourFrom, final String[] tourTo); 
} 

我的服务实现:

@Service("assignFAServices") 
public class AssignFAServices implements AssignFAServicesInterface{ 
@Autowired 
AssignFADaoInterface assignFADaoInterface; 
@Override 
@Transactional(rollbackFor = java.lang.Exception.class) 
public int saveAssignedFA(final String[] CVListCode, final int userCodeFA, final String randomNoColumn, final String[] tourFrom, final String[] tourTo) { 
    return assignFADaoInterface.saveAssignedFA(CVListCode,userCodeFA,randomNoColumn,tourFrom,tourTo); 
} 

吾道Implementa重刑是

@Repository("assignFADao") 
public class AssignFADao implements AssignFADaoInterface{ 

private JdbcTemplate jdbcTemplate; 

@Autowired 
public void setDataSource(DataSource dataSource) { 
    this.jdbcTemplate = new JdbcTemplate(dataSource); 
} 
@Override 
public int saveAssignedFA(final String[] CVListCode, final int userCodeFA, final String randomNoColumn, final String[] tourFrom, final String[] tourTo) { 
    int successCV[]; 
    int successSV[]; 
    int flag = 0; 
    String sqlUpdateCV = ""; 
    String sqlUpdateSV = ""; 
    try { 
     sqlUpdateCV = "UPDATE cce_cropvillagelist SET usercodefa=? WHERE cvlistcode=?"; 
     sqlUpdateSV = "UPDATE cce_samplevillages SET randomnocolumn=?,tourfrom=?,tourto=?,levelreached='FA',statusofdocument='F' WHERE cvlistcode=?"; 
     successCV = jdbcTemplate.batchUpdate(sqlUpdateCV, new BatchPreparedStatementSetter() { 

      @Override 
      public void setValues(PreparedStatement ps, int i) throws SQLException { 
       ps.setInt(1, userCodeFA); 
       ps.setInt(2, Integer.parseInt(CVListCode[i])); 
      } 

      @Override 
      public int getBatchSize() { 
       return CVListCode.length; 
      } 
     }); 
     int a=Integer.valueOf("bvxv"); 
     if (successCV.length > 0) { 
      flag = 1; 
      successSV = jdbcTemplate.batchUpdate(sqlUpdateSV, new BatchPreparedStatementSetter() { 

       @Override 
       public void setValues(PreparedStatement ps, int i) throws SQLException { 
        if (!tourFrom[i].trim().equals("-")) { 
         ps.setString(1, randomNoColumn); 
         ps.setDate(2, tourFrom[i].length()==0?null:new java.sql.Date(DateAndTime.convertStringToDate(tourFrom[i]).getTime())); 
         ps.setDate(3, tourTo[i].length()==0?null:new java.sql.Date(DateAndTime.convertStringToDate(tourTo[i]).getTime())); 
         ps.setInt(4, Integer.parseInt(CVListCode[i])); 
        } 
       } 

       @Override 
       public int getBatchSize() { 
        return CVListCode.length; 
       } 
      }); 
      if (flag == 1) { 
       System.out.println("FLAG"+flag); 
      } 
     } 
    } catch (NumberFormatException | DataAccessException ex) { 
     System.out.println("Exception in AssignFADao.saveAssignedFA(final int[] CVListCode, final int userCodeFA,final String randomNoColumn,final Date[] tourFrom,final Date[] tourTo) : " + ex); 
    } finally { 
    } 
    return flag; 
} 

请帮助。 首先,当我尝试使用@Transactional时,项目没有运行,然后询问了一个jar包。添加jar后,项目正在运行,但不会发生回滚。

+0

不要捕捉异常或从catch块重新抛出。 – Lovababu

+0

我尝试删除try catch块,但仍然执行第一个Update语句并且没有发生回滚。此外,我试图重新抛出错误,但同样的事情发生 – zee

+0

不要处理异常,只需将方法签名添加抛出异常。 – Lovababu

回答

1

当您使用

@Transactional(rollbackFor = java.lang.Exception.class) 

你人告诉春磨片的抛出异常是回滚事务,但你抓住他们

catch (NumberFormatException | DataAccessException ex) { 
     System.out.println("Exception in AssignFADao.saveAssignedFA(final int[] CVListCode, final int userCodeFA,final String randomNoColumn,final Date[] tourFrom,final Date[] tourTo) : " + ex); 
    } 

所以春天从来没有注意到的例外是抛出。您可以删除try/catch块,或者你可以再次抛出异常的try/catch块

catch (NumberFormatException | DataAccessException ex) { 
    System.out.println("Exception in AssignFADao.saveAssignedFA(final int[] CVListCode, final int userCodeFA,final String randomNoColumn,final Date[] tourFrom,final Date[] tourTo) : " + ex); 
    throw ex; 
} 

UPDATE

我认为你缺少注解驱动的标签在你的配置文件中。

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

在这篇文章里有一个你正在做什么的完整例子。

http://www.journaldev.com/2603/spring-transaction-management-example-with-jdbc

+0

我尝试删除try catch块,但仍然执行第一个Update语句并且没有发生回滚。此外,我试图重新抛出错误,但同样的事情发生了 – zee

+0

我编辑了答案 – reos

+0

谢谢你的回答。但它仍然没有工作..我真的不明白为什么这样? – zee