[英]Spring jdbcTemplate @transactional does not rollback for postgres
我正在嘗試在我的項目中使用@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后,該項目正在運行,但不會發生回滾。
使用時
@Transactional(rollbackFor = java.lang.Exception.class)
您是在告訴Spring拋出異常時回滾事務,但是您正在捕獲它們
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;
}
UPDATE
我認為您在配置文件中缺少注釋驅動的標簽。
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
在本文中,有一個您所做的完整示例。
http://www.journaldev.com/2603/spring-transaction-management-example-with-jdbc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.