Java 带有事务提交的Spring JDBC失败
我正在使用事务处理SpringJDBC项目。 我使用PostgreSQL作为数据库: 以下是我的spring配置:Java 带有事务提交的Spring JDBC失败,java,spring,postgresql,jdbc,Java,Spring,Postgresql,Jdbc,我正在使用事务处理SpringJDBC项目。 我使用PostgreSQL作为数据库: 以下是我的spring配置: <tx:annotation-driven/> <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource
<tx:annotation-driven/>
<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceMainDatabase"/>
</bean>
<bean id="dataSourceMainDatabase" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/db_toto"/>
<property name="username" value="toto"/>
<property name="password" value="toto"/>
<property name="defaultAutoCommit" value="false" />
<property name="maxActive" value="5"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="16000"/>
<property name="minIdle" value="0"/>
</bean>
DAO代码如下所示:
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly=false)
public void synchronize( Exobox exobox ) {
daoExotool.updateDerniereSynchro(exobox,new Date(),new Date(),true,10);
}
public void updateDerniereSynchro(Exobox exobox, Date dateFinSynchro,
Date dateSynchroDerniereDonnee, boolean synchroOk, int nbPositions) {
getJdbcTemplate().update(sqlInsertSynchro,exobox.getExoboxId(),dateFinSynchro,
dateSynchroDerniereDonnee,nbPositions,synchroOk);
}
使用的查询如下所示:
private final String sqlInsertSynchro =
"INSERT INTO t_exotool_synchro_log("+
"synchro_log_exobox_id, synchro_log_date_fin,synchro_log_date_data,"
+ "synchro_log_nb_positions,synchro_log_ok)"+
"VALUES (?, ?, ?, ? , ?)";
synchronize方法本身是从另一个类调用的,如下所示:
public class DbInserts {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
exoboxYgos = context.getBean("exoboxYgos",Exobox.class);
synchro = context.getBean(IServiceSynchro.class);
context.close();
synchro.synchronize(exoboxYgos);
}
}
启动应用程序时,日志会显示以下内容:
DEBUG [ 13 août 2014 10:52:56 ] Creating new transaction with name [fr.axione.exotool.service.ImplServiceSynchro.synchronize]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; ''
DEBUG [ 13 août 2014 10:52:56 ] Acquired Connection [jdbc:postgresql://localhost/db_exotool, UserName=toto, PostgreSQL Native Driver] for JDBC transaction
DEBUG [ 13 août 2014 10:52:56 ] Executing prepared SQL update
DEBUG [ 13 août 2014 10:52:56 ] Executing prepared SQL statement [INSERT INTO t_exotool_synchro_log(synchro_log_exobox_id, synchro_log_date_fin,synchro_log_date_data,synchro_log_nb_positions,synchro_log_ok)VALUES (?, ?, ?, ? , ?)]
DEBUG [ 13 août 2014 10:52:56 ] Fetching JDBC Connection from DataSource
DEBUG [ 13 août 2014 10:52:56 ] Registering transaction synchronization for JDBC Connection
DEBUG [ 13 août 2014 10:52:56 ] SQL update affected 1 rows
DEBUG [ 13 août 2014 10:52:56 ] Returning JDBC Connection to DataSource
DEBUG [ 13 août 2014 10:52:56 ] Initiating transaction commit
DEBUG [ 13 août 2014 10:52:56 ] Committing JDBC transaction on Connection [jdbc:postgresql://localhost/db_exotool, UserName=toto, PostgreSQL Native Driver]
DEBUG [ 13 août 2014 10:52:56 ] Releasing JDBC Connection [jdbc:postgresql://localhost/db_exotool, UserName=toto, PostgreSQL Native Driver] after transaction
DEBUG [ 13 août 2014 10:52:56 ] Returning JDBC Connection to DataSource
我的问题是,插入的行从未存储在数据库本身中
另一方面,如果我设置:
<property name="defaultAutoCommit" value="true" />
插入有效,但回滚无效
有人有主意吗
多谢各位,
Emmanuel。您的aoExotool.updateDerniereSynchro正在做什么?你能帮我发一下密码吗?你好,谢谢。我在问题中发布了我的DAO代码。我尝试在没有注释的情况下实现事务机制,手动使用PlatformTransactionManager,这样一切都很好。也许我的问题在于,请发布直接调用的代码:
public void synchronize(Exobox Exobox)
-它是否与synchronize
方法在同一个类中?嗨,拉尔夫:我在原始问题中发布了调用synchronize方法的代码。谢谢。关闭上下文将导致清理资源(如关闭数据源、销毁事务管理器等)。您基本上失去了正确运行应用程序所需的一些bean。您要做的基本上是强制关闭MS Word,然后尝试保存文档,然后发现您所做的更改不存在。