我有一个使用spring 4.0,JPA 2.1,Hibernate 4.3.5,Glassfish 4.0和JTA事务管理器的应用程序。
在我将JPA从2.0更新到2.1并从Hibernate 4.2.x更新到4.3后,数据不再保存到数据库中,尽管日志似乎没问题。
所以这些可能是配置文件:
entityManager
<bean id="emfCC" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="CCDS" />
<property name="packagesToScan" value="com.videanuadrian.core.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.connection.autocommit">false </prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.ehcache.EhCacheProvider</prop>
<prop key="hibernate.generate_statistics">false</prop>
</props>
</property>
我的交易经理:
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:appserver/TransactionManager"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes >
<tx:method name="get*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="true"/>
<tx:method name="is*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy/>
<aop:config>
<aop:pointcut id="cs" expression="execution(* com.videanuadrian.contactcenter.services..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="cs" />
</aop:config>
<aop:config>
<aop:pointcut id="pcCore" expression="execution(* com.videanuadrian.core.impl.services..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pcCore"/>
</aop:config>
<jee:jndi-lookup id="CCDS" jndi-name="jdbc/contactCenter"/>
我所有的DAO课程都在这堂课中:
public abstract class GenericDAOImpl<T> implements GenericDAO<T> {
@PersistenceContext(type = PersistenceContextType.TRANSACTION,synchronization=SynchronizationType.SYNCHRONIZED)
protected EntityManager em;
private Class<T> type;
protected static final Logger logger = Logger.getLogger(GenericDAOImpl.class);
public GenericDAOImpl() {
Type t = getClass().getGenericSuperclass();
ParameterizedType pt = (ParameterizedType) t;
type = (Class) pt.getActualTypeArguments()[0];
}
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
@Override
public T create(final T t) {
this.em.persist(t);
return t;
}
@Override
public void delete(final Object id) {
this.em.remove(this.em.getReference(type, id));
}
@Override
public T find(final Object id) {
return (T) this.em.find(type, id);
}
@Override
public T update(final T t) {
return this.em.merge(t);
}
@Override
public void flush(){
this.em.flush();
}
}
这是我的服务类:
@Named
public class ApplicationServiceImpl implements ApplicationService {
@Inject
private ApplicationDAO applicationDAO;
@Override
public Long updateApplication(Application app) {
if (app == null)
return null;
applicationDAO.update(app);
return app.getId();
}
}
在我点击updateApplication方法后,日志是正确的,一切似乎都正常,但更新不会持久保存到数据库。起初,这似乎是事务没有或事务没有创建,但这里是日志:
AbstractPlatformTransactionManager.java:367) - Creating new transaction with name [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT,-java.lang.Exception
(TransactionSynchronizationManager.java:272) - Initializing transaction synchronization
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]
(EntityManagerFactoryUtils.java:272) - Opening JPA EntityManager
(EntityManagerFactoryUtils.java:290) - Registering transaction synchronization for JPA EntityManager
(TransactionSynchronizationManager.java:193) - Bound value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] to thread [http-listener-1(3)]
(AbstractBeanFactory.java:249) - Returning cached instance of singleton bean 'auditLogAspectImpl'
(AbstractPlatformTransactionManager.java:472) - Participating in existing transaction
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.UserServiceImpl.getUser]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.UserServiceImpl.getUser]
(AbstractPlatformTransactionManager.java:472) - Participating in existing transaction
(TransactionAspectSupport.java:447) - Getting transaction for [com.videanuadrian.core.impl.services.AuditLogServiceImpl.addAuditLogEvent]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionSynchronizationManager.java:140) - Retrieved value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] bound to thread [http-listener-1(3)]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.AuditLogServiceImpl.addAuditLogEvent]
(TransactionAspectSupport.java:476) - Completing transaction for [com.videanuadrian.core.impl.services.ApplicationServiceImpl.updateApplication]
(AbstractPlatformTransactionManager.java:926) - Triggering beforeCommit synchronization
(AbstractPlatformTransactionManager.java:939) - Triggering beforeCompletion synchronization
(TransactionSynchronizationManager.java:243) - Removed value [org.springframework.orm.jpa.EntityManagerHolder@48ff5d9c] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@e9c2514] from thread [http-listener-1(3)]
(EntityManagerFactoryUtils.java:435) - Closing JPA EntityManager
(AbstractPlatformTransactionManager.java:755) - Initiating transaction commit
(AbstractPlatformTransactionManager.java:952) - Triggering afterCommit synchronization
(AbstractPlatformTransactionManager.java:968) - Triggering afterCompletion synchronization
(TransactionSynchronizationManager.java:331) - Clearing transaction synchronization
(RequestLoggingPhaseListener.java:42) - Entering JSF Phase: RESTORE_VIEW 1
(RequestLoggingPhaseListener.java:42) - Entering JSF Phase: RENDER_RESPONSE 6
如果启用sql_log,我看不到正在执行的更新语句....
我怀疑JPA 2.1之间的任何我的entityManager,事务管理器,但我无法弄清楚它是什么...
有什么想法吗?
答案 0 :(得分:0)
我应用的解决方案是取消persistence.xml文件并在那里添加属性:
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
发现于:
https://coderwall.com/p/e5fxrw
我也从我的EntityManager定义中删除了packagesToScan。
答案 1 :(得分:0)
将Hibernate从4.2.x更新到4.3.x后,我遇到了同样的问题。
设置&#34; hibernate.transaction.flush_before_completion&#34;属性为真正解决了我的问题。
<prop key="hibernate.transaction.flush_before_completion">true</prop>
https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/
&#34;如果启用,会话将在事务完成前阶段自动刷新。内置和自动会话上下文管理是首选,请参见第2.5节“上下文会话”&#34;。