gpt4 book ai didi

java - Spring jdbcTemplate @transactional 不回滚 postgres

转载 作者:行者123 更新时间:2023-11-29 13:25:45 26 4
gpt4 key购买 nike

我正在尝试在我的项目中使用 @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>

我的服务接口(interface)是:

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 block ,也可以在 try/catch block 内再次抛出异常

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

关于java - Spring jdbcTemplate @transactional 不回滚 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34039545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com