【问题标题】:Why aren't my transactions getting commited?为什么我的交易没有被提交?
【发布时间】:2014-12-10 21:53:08
【问题描述】:

当我将 defaultAutoCommit 设置为 false 时,我的事务不会保存到数据库中。 有人知道为什么我的数据没有保存到数据库中吗?

我在我的方法上使用@transactional 似乎正在提交事务,因为状态为 3

交易状态 https://docs.jboss.org/jbossas/javadoc/4.0.2/javax/transaction/Status.java.html

这是交易记录

2014-12-10 14:39:07,132 TRACE [http-apr-8080-exec-2] CacheSynchronization - 完成回调之前的事务

2014-12-10 14:39:07,132 TRACE [http-apr-8080-exec-2] CacheSynchronization - 完成回调后的事务,状态:3

<aop:aspectj-autoproxy/>
<context:annotation-config/>

<!-- ******************************************************************** -->
<!-- Scan for dao layer annotated beans -->
<!-- ******************************************************************** -->
<context:component-scan base-package="package.dao" scoped-proxy="interfaces"/>

<!-- ******************************************************************** -->
<!-- Mark bean transactions as annotation driven -->
<!-- ******************************************************************** -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- ******************************************************************** -->
<!-- PropertyConfigurer for the DAO -->
<!-- ******************************************************************** -->
<context:property-placeholder location="classpath:prop-dao.properties"/>        

<!-- ******************************************************************** -->
<!-- Setup the transaction manager -->
<!-- ******************************************************************** -->
    <!-- Using Atomikos Transaction Manager -->
    <bean class="com.atomikos.icatch.jta.UserTransactionManager" destroy-method="close" id="atomikosTransactionManager" init-method="init">
        <property name="forceShutdown" value="true"/>
        <property name="startupTransactionService" value="true"/>
        <property name="transactionTimeout" value="600000"/>
    </bean>

    <bean class="com.atomikos.icatch.jta.UserTransactionImp" id="atomikosUserTransaction"/>

    <!-- Configure the Spring framework to use JTA transactions from Atomikos -->
    <bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
        <property name="transactionManager" ref="atomikosTransactionManager"/>
        <property name="userTransaction" ref="atomikosUserTransaction"/>
        <property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION"/>
    </bean>

<!-- ******************************************************************** -->
<!-- Setup a data source -->
<!-- ******************************************************************** -->




            <!-- Using Apache DBCP Data Sources -->
            <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" name="propDS">
                <property name="driverClassName" value="${prop.connection.driver_class}"/>
                <property name="username" value="${prop.connection.username}"/>
                <property name="password" value="${prop.connection.password}"/>
                <property name="url" value="${prop.connection.url}"/>
                <property name="maxIdle" value="${prop.minPoolSize}"/>
                <property name="maxActive" value="${prop.maxPoolSize}"/>
                <property name="defaultAutoCommit" value="false"/>
            </bean>



<!-- ******************************************************************** -->
<!-- Setup each persistence unit -->
<!-- ******************************************************************** -->



            <!-- Configure a JPA vendor adapter -->
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="propJPAVendorAdapter">
                <property name="showSql" value="${prop.show_sql}"/>
                <property name="generateDdl" value="${prop.generateDdl}"/>
                <property name="databasePlatform" value="${prop.dialect}"/>
            </bean>
            <!-- EntityManager Factory that brings together the persistence unit, datasource, and JPA Vendor -->
            <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="prop">
                <property name="dataSource" ref="propDS"/>
                <property name="persistenceUnitName" value="prop"/>
                <property name="jpaVendorAdapter" ref="propJPAVendorAdapter"/>
                    <property name="jpaPropertyMap">
                        <map>
                                    <entry key="hibernate.transaction.manager_lookup_class" value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup"/>
                                    <entry key="hibernate.connection.release_mode" value="on_close"/>
                        </map>
                    </property>
            </bean>

编辑: 我尝试在方法结束时手动提交,但数据仍未保存

【问题讨论】:

    标签: java spring hibernate transactions autocommit


    【解决方案1】:

    当我将 defaultAutoCommit 设置为 false 时,我的交易不会被保存 到数据库

    这是将 defaultAutoCommit 设置为 false 的目的。 将其设置为 true,它将自动提交。 设置为false需要手动提交,否则会被回滚。

    【讨论】:

    • @Autowired 应该在事务结束时提交。我尝试在我的方法结束时手动提交,但数据仍然没有保存,我收到一个错误,说事务已经完成可能回滚。 org.springframework.transaction.UnexpectedRollbackException: JTA transaction already completed - probably rolled back
    • @user2631291 不,不是
    • 有没有办法查看事务是否回滚?
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2017-01-27
    • 2014-04-08
    相关资源
    最近更新 更多