postgresql 上的事务未提交,映射对象未使用 autoCommit=false 进行持久化
在正在运行的事务中调用 persist 时,映射对象不会持久保存在 DB (Postgresql 8.4) 中。我正在使用 Spring 事务管理
org.springframework.jdbc.datasource.DataSourceTransactionManager
所以一切都应该没问题。我将数据源上的自动提交模式设置为“false”。当将模式设置为“true”时,将完成提交(并且对象将被持久化),但这会导致更大的问题(例如从数据库获取 blob)。所以我必须将自动提交模式设置为“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);
}
这是调用时的日志调用数据库调用的服务方法:
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
如您所见,事务正在提交(根据日志),但数据库中没有持久化对象(尽管正在执行插入和更新)。
将我的数据源配置中的提交模式设置为
属性名称=“defaultAutoCommit”值=“true”
一切正常!
我真的不知道是什么导致了这个奇怪的问题......如果有人能给我提示,我会很高兴。
Mapped objects are not being persisted in DB (Postgresql 8.4) when calling persist in a running transaction. I am using Spring Transaction Management with the
org.springframework.jdbc.datasource.DataSourceTransactionManager
so everything should be fine. I set the autocommit-mode on the DataSource to "false". When setting the mode to "true" the commit will be done (and objects are persisted), but that leads to bigger problems (eg fetching blobs from db). So I have to set the autocommit-mode to "false", which is also the prefered mode that everybody told me...
This is my persistence configuration (I reduced the code to its necessary stuff):
<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 -->
I should also mention that I am using 3 different transaction managers (with of course 3 different datasources)...
My own transaction annotation that will be reflected by the qualifier attribute mentioned above:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("auth")
public @interface AuthenticationTX{}
Annotated service class that "should" persist the object...
@AuthenticationTX
@Override
public void loginClient(Client cl) {
// do stuff
// call dao.persist(cl);
}
This is the log when calling the service method that invokes a db call:
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
As you can see, the transaction is being committed (according to the log), but no object is being persisted in db (although the insert and update are being executed).
When setting the commit mode in my datasource configuration to
property name="defaultAutoCommit" value="true"
everything works fine!
I really dont know what causes this curious problem... I'd be glad if anyone could give me a hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用 Hibernate,您需要一个 HibernateTransactionManager:
To work with Hibernate you need a
HibernateTransactionManager
: