gpt4 book ai didi

spring - Hibernate Envers 未找到 JPA 事务 :

转载 作者:行者123 更新时间:2023-12-01 14:36:57 24 4
gpt4 key购买 nike

我一整天都在努力思考这个问题。

目前,我们的项目已通过 Spring Application Context 设置 JPATransactionManager 来处理我们的各种 session 事务,并在处理持久性和删除(DAO 使用)的所有服务上使用@Transactional。

从 Hibernate 3 切换到 5,我们想取消对自定义审计拦截器的使用,转而使用 Hibernate Envers。我已经正确地注释了我所有的类并创建了表,但是一旦它真正到达插入点,监听器就会抛出一个错误,它无法找到 JPA 给出的当前事务:

org.hibernate.envers.exception.AuditException: Unable to create revision because of non-active transaction    at org.hibernate.envers.event.spi.BaseEnversEventListener.checkIfTransactionInProgress(BaseEnversEventListener.java:132)    at org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl.onPostInsert(EnversPostInsertEventListenerImpl.java:34)    at org.hibernate.action.internal.EntityIdentityInsertAction.postInsert(EntityIdentityInsertAction.java:156)    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:102)    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:97)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:651)    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:643)    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:638)

Looking inside the code, it seems that it's basing the transaction status off it's default value of INACTIVE meaning that it's not hooking into the transaction properly. I know that Hibernate Envers also automatically pushes the listeners into hibernate with recent versions so I don't know if this may also be a source of the issue.

I know that its been documented to work with HibernateTransactionManager but we wish to step away from using that in favor of hooking up our transactions and sessions solely via Spring making things easier so it may also be the need of finding an alternative to envers. Does anyone have any advice or solutions to this problem? Or also hit this issue?

ApplicationContext.xml

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref=“dataSource" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbcx.JtdsDataSource" />
<property name="url" value="jdbc:jtds:sqlserver://.." />
<property name="username" value=“..." />
<property name="password" value=“..." />
</bean>

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>classpath:hibernate.properties</value>
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<ref bean="hibernateProperties" />
</property>
</bean>

<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_SUPPORTS,readOnly
</prop>
<prop key="load*">PROPAGATION_SUPPORTS,readOnly
</prop>
<prop key="make*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="refresh">PROPAGATION_SUPPORTS</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_SUPPORTS,readOnly
</prop>
</props>
</property>
</bean>

<bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="transactionManager" />
</bean>

hibernate 属性

#hibernate.hbm2ddl.auto=update
hibernate.show_sql=true

hibernate.connection.datasource=java\:comp/env/datasource
#hibernate.connection.provider_class=org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl
hibernate.connection.provider_class=org.hibernate.connection.DatasourceConnectionProvider

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
#hibernate.generate_statistics=true
hibernate.cache.use_structured_entries=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.id.new_generator_mappings=false

hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect

hibernate.listeners.envers.autoRegister=false
org.hibernate.envers.track_entities_changed_in_revision=false
org.hibernate.envers.audit_table_prefix=AUD_
org.hibernate.envers.audit_table_suffix=

我的 DAO 像这样使用 txProxyTemplate 连接起来

<bean id="objectDAO" parent="txProxyTemplate">
<property name="target">
<bean
class="path.to.objectDAOImpl">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</property>
</bean>

我所有使用各种 DAO 的服务都使用 @Transactional 注释简单地连接到我们想要进行交易的地方。我已经能够通过跟踪看到我的交易成功完成并在出现错误时回滚。一旦我将 envers 添加到组合中,审计就找不到要加入的事务。一定有什么我想念的,但我不确定它是什么。

最佳答案

我认为您不需要定义 txProxyTemplate bean 也不是 SpringTransactionPolicy根据我的经验。此功能已被 <tx:/> 取代标签和 @Transactional 的使用注释。

您只需要确保 JpaTransactionManager已创建并关联为 transactionManager<tx:annotation-driven/> 相关联标签。

关于spring - Hibernate Envers 未找到 JPA 事务 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38840794/

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