【问题标题】:Transaction on postgresql not committed, mapped objects are not being persisted with autoCommit=falsepostgresql 上的事务未提交,映射对象未通过 autoCommit=false 持久化
【发布时间】:2010-09-02 15:12:04
【问题描述】:

在正在运行的事务中调用 persist 时,映射的对象不会被持久化在 DB (Postgresql 8.4) 中。我正在使用带有

的 Spring 事务管理

org.springframework.jdbc.datasource.DataSourceTransactionManager

所以一切都应该没问题。我将 DataSource 上的自动提交模式设置为“false”。当将模式设置为“true”时,将完成提交(并且对象被持久化),但这会导致更大的问题(例如,从数据库中获取 blob)。所以我必须将 autocommit-mode 设置为“false”,这也是大家告诉我的首选模式...

这是我的持久化配置(我将代码简化为必要的东西):

<bean id="authDatabase" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/authentication_db" />
    <property name="username" value="test"/>
    <property name="password" value="test"/>
    <property name="defaultAutoCommit" value="false" />               
</bean>

<bean id="serviceInfoSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="authDatabase" />
    <!-- Very important for transactional usage with org.springframework.jdbc.datasource.DataSourceTransactionManager -->
    <property name="useTransactionAwareDataSource" value="true"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <!-- mapped objects, not necessary --->
        </list>
    </property>
</bean>

<!-- defaults to transactionManager -->
<tx:annotation-driven/>

<bean id="authTXManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="authDatabase"/> 
    <qualifier value="auth"/>
</bean>

<!-- more stuff -->

我还应该提到我正在使用 3 个不同的事务管理器(当然还有 3 个不同的数据源)...

上面提到的qualifier属性会体现我自己的事务注解:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("auth")
public @interface AuthenticationTX{}

“应该”保留对象的带注释的服务类...

@AuthenticationTX
@Override
public void loginClient(Client cl) {
    // do stuff
    // call dao.persist(cl);
}

这是调用db调用的服务方法时的日志:

16:21:24,031 DEBUG DataSourceTransactionManager:365 - Creating new transaction with name [com.example.ILoginManager.loginClient]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
16:21:24,078 DEBUG DataSourceTransactionManager:205 - Acquired Connection [jdbc:postgresql://localhost:5432/authentication_db, UserName=auth_user, PostgreSQL Native Driver] for JDBC transaction
Hibernate: select user0_.id as id14_, user0_.email as email14_, user0_.firstname as firstname14_, user0_.isLocked as isLocked14_, user0_.lastLogin as lastLogin14_, user0_.lastname as lastname14_, user0_.loginname as loginname14_, user0_.organisation_id as organis10_14_, user0_.passwort as passwort14_, user0_.userGUID as userGUID14_ from UserAccount user0_ where user0_.loginname=?
Hibernate: select client0_.id as id3_, client0_.clientId as clientId3_, client0_.clientType as clientType3_, client0_.connectedAt as connecte4_3_, client0_.language as language3_, client0_.screenHeight as screenHe6_3_, client0_.screenWidth as screenWi7_3_, client0_.securityToken as security8_3_, client0_.user_id as user9_3_ from Client client0_ where client0_.user_id=?
Hibernate: select user0_.id as id14_, user0_.email as email14_, user0_.firstname as firstname14_, user0_.isLocked as isLocked14_, user0_.lastLogin as lastLogin14_, user0_.lastname as lastname14_, user0_.loginname as loginname14_, user0_.organisation_id as organis10_14_, user0_.passwort as passwort14_, user0_.userGUID as userGUID14_ from UserAccount user0_ where user0_.loginname=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into Client (clientId, clientType, connectedAt, language, screenHeight, screenWidth, securityToken, user_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update UserAccount set email=?, firstname=?, isLocked=?, lastLogin=?, lastname=?, loginname=?, organisation_id=?, passwort=?, userGUID=? where id=?
16:21:24,187 DEBUG DataSourceTransactionManager:752 - Initiating transaction commit
16:21:24,187 DEBUG DataSourceTransactionManager:265 - Committing JDBC transaction on Connection [jdbc:postgresql://localhost:5432/authentication_db, UserName=auth_user, PostgreSQL Native Driver]
16:21:24,187 DEBUG DataSourceTransactionManager:323 - Releasing JDBC Connection [jdbc:postgresql://localhost:5432/authentication_db, UserName=auth_user, PostgreSQL Native Driver] after transaction

如您所见,事务正在提交(根据日志),但没有对象被持久化在 db 中(尽管正在执行插入和更新)。

在我的数据源配置中将提交模式设置为

property name="defaultAutoCommit" value="true"

一切正常!

我真的不知道是什么导致了这个奇怪的问题......如果有人能给我一个提示,我会很高兴。

【问题讨论】:

    标签: hibernate spring postgresql transactional


    【解决方案1】:

    要使用 Hibernate,您需要 HibernateTransactionManager

    <bean id="authTXManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="serviceInfoSessionFactory"/>  
        <qualifier value="auth"/> 
    </bean> 
    

    【讨论】:

    • 嗯,明天在办公室试试这个。以为我已经尝试过了但没有成功...您确定我不能使用spring提供的DataSourceTransactionManager(.jdbc.datasource)吗?这两位经理有什么区别?
    • @tim:也许我的建议是错误的,因为我没有注意到useTransactionAwareDataSource=true。你真的需要它吗?
    • 嗯,实际上我刚刚读到使用“jdbc.datasource.DataSourceTransactionManager”时必须使用此属性。我将事务管理器更改为您提到的那个,它真的对我有用!你知道这两位经理的区别吗?日志说(与两个管理器)语句被发送到数据库(插入/更新)并且事务被提交......所以我不知道为什么使用“jdbc.datasource.DataSourceTransactionManager”的方法不起作用...... .
    • @tim:当您使用没有 Spring 事务的独立 Hibernate 时,您使用 Hibernate 的 commit 方法提交事务。 HibernateTransactionManager 对 Spring 管理的事务做同样的事情,所以当事务将要提交并且可以完成它的工作时,Hibernate 会收到通知。 useTransactionAwareDataSource=trueDataSourceTransactionManager 是针对特殊情况的高级功能(请参阅文档),所以我不知道为什么它不起作用。
    • 好的,我会做一些研究。但目前它对我有用,所以感谢您的解释和帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2014-07-15
    相关资源
    最近更新 更多