Spring jdbcTemplate @transactional不会回滚postgres

时间:2015-12-02 09:49:09

标签: java spring postgresql transactions

我正在尝试在我的项目中使用@Transactional,我在一个方法中有两个DML语句。我的第一个DML语句即使在第一个语句之后就有异常也会得到优先。我希望它能够回滚。我该怎么办?我在很多方面尝试过但没有帮助。

我的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);
}

我的DAO实施

@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时,项目没有运行,然后要求提供aopalliance jar。添加该jar后,项目正在运行,但不会发生回滚。

1 个答案:

答案 0 :(得分:1)

使用时

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

你告诉Spring,当抛出Exception但是你正在捕获它们时回滚事务

    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);
    }

所以Spring从未注意到抛出异常。您可以删除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;
}

<强>更新

我认为你的配置文件中缺少注释驱动的标记。

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

在这篇文章中,有一个完整的示例,说明了你在做什么。

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

相关问题