好吧,我的项目是使用EJB和JPA(使用Hibernate),但我不得不切换到Spring。 在那之前一切都运转良好。 EJB用于注入EntityManager,控制事务等。
好的,当我切换到Spring时,我遇到了很多问题,因为我是Spring的新手。但是在一切运行之后,我遇到了问题:数据永远不会保存在数据库中。我配置我的Spring来控制事务,我在JSF中使用了spring bean,它有弹簧服务来完成这项工作。此服务注入了EntityManager并使用@Transactional REQUIRED。此服务将EntityManager传递给调用entityManager.persist(bean)的DAO。
选择似乎运行良好,JTA事务似乎运行良好(我在日志中看到),但实体未保存!
这是日志:
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter: doFilterInternal() (linha 136): Opening JPA EntityManager in OpenEntityManagerInViewFilter
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory: doGetBean() (linha 245): Returning cached instance of singleton bean 'transactionManager'
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: getTransaction() (linha 365): Creating new transaction with name [br.org.cni.pronatec.controller.service.MontanteServiceImpl.adicionarValor]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 493): Opened new Session [org.hibernate.impl.SessionImpl@2b2fe2f0] for Hibernate transaction
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 504): Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@2b2fe2f0]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doBegin() (linha 569): Exposing Hibernate transaction as JDBC transaction [com.sun.gjc.spi.jdbc40.ConnectionHolder40@3bcd4840]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler: doJoinTransaction() (linha 383): Joined JTA transaction
INFO: Hibernate:
select
hibernate_sequence.nextval
from
dual
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: processCommit() (linha 752): Initiating transaction commit
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doCommit() (linha 652): Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@2b2fe2f0]
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.HibernateTransactionManager: doCleanupAfterCompletion() (linha 734): Closing Hibernate Session [org.hibernate.impl.SessionImpl@2b2fe2f0] after transaction
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.hibernate3.SessionFactoryUtils: closeSession() (linha 800): Closing Hibernate Session
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter: doFilterInternal() (linha 154): Closing JPA EntityManager in OpenEntityManagerInViewFilter
INFO: [Pronatec] - 04/04/2012 11:30:20 - [DEBUG] org.springframework.orm.jpa.EntityManagerFactoryUtils: closeEntityManager() (linha 343): Closing JPA EntityManager
在日志中,我看到它提交了事务,但我没有看到插入查询(Hibernate正在打印任何查询)。我还看到Hibernate查找以获取序列ID的下一个值。但在那之后,它永远不会真正插入。
这是弹簧上下文配置:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="PronatecPU" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="transactionManagerName" value="java:/TransactionManager" />
<property name="userTransactionName" value="UserTransaction" />
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager" />
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="PronatecPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/pronatec</jta-data-source>
<class>br.org.cni.pronatec.model.bean.AgendamentoBuscaSistec</class>
<class>br.org.cni.pronatec.model.bean.AgendamentoExportacaoZeus</class>
<class>br.org.cni.pronatec.model.bean.AgendamentoImportacaoZeus</class>
<class>br.org.cni.pronatec.model.bean.Aluno</class>
<class>br.org.cni.pronatec.model.bean.Curso</class>
<class>br.org.cni.pronatec.model.bean.DepartamentoRegional</class>
<class>br.org.cni.pronatec.model.bean.Dof</class>
<class>br.org.cni.pronatec.model.bean.Escola</class>
<class>br.org.cni.pronatec.model.bean.Inconsistencia</class>
<class>br.org.cni.pronatec.model.bean.Matricula</class>
<class>br.org.cni.pronatec.model.bean.Montante</class>
<class>br.org.cni.pronatec.model.bean.ParametrosVingentes</class>
<class>br.org.cni.pronatec.model.bean.TipoCurso</class>
<class>br.org.cni.pronatec.model.bean.Turma</class>
<class>br.org.cni.pronatec.model.bean.UnidadeFederativa</class>
<class>br.org.cni.pronatec.model.bean.ValorAssistenciaEstudantil</class>
<class>br.org.cni.pronatec.model.bean.ValorHora</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="current_session_context_class" value="thread"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
这是我在托管bean中注入的服务:
@Service
@Scope("prototype")
@Transactional(propagation= Propagation.REQUIRED)
public class MontanteServiceImpl {
// more code
@PersistenceContext(unitName="PronatecPU", type= PersistenceContextType.EXTENDED)
private EntityManager entityManager;
// more code
// The method that is called by another public method that do something before
private void salvarMontante(Montante montante) {
montante.setDataTransacao(new Date());
MontanteDao montanteDao = new MontanteDao(entityManager);
montanteDao.salvar(montante);
}
// more code
}
My MontanteDao继承了一个基础DAO,就像这样:
public class MontanteDao extends BaseDao<Montante> {
public MontanteDao(EntityManager entityManager) {
super(entityManager);
}
}
在BaseDao中调用的方法是:
public void salvar(T bean) {
entityManager.persist(bean);
}
就像你看到的那样,它只需选择注入的entityManager并调用persist()方法。该事务由Spring控制,就像打印在日志中一样,但插入查询永远不会打印在日志中,也永远不会保存。
我对自己糟糕的英语感到抱歉。 在此先感谢谁帮助。
编辑: 调试,我看到在每个entityManager.persist(T)之后,在“insert”类型的actionQueue中排队了另一个动作。毕竟,它只是排队行动而且从不执行它们。有没有人遇到过这种问题?
答案 0 :(得分:0)
尝试强制flush到数据库。
entityManager.flush();
答案 1 :(得分:0)
好吧,我终于解决了我的问题。 JTA存在一些错误配置。我不知道会是什么,但我改为RESOURCE_LOCAL并且它有效。
我第一次尝试RESOURCE_LOCAL时,我遇到了Glassfish验证@PersistenceContext注释并抛出“无法解析与persistence-context-ref-name [...]相对应的持久性单元”的问题。然后我找到了一种禁用此验证的好方法:指定
metadata-complete=true
在我的web.xml上。它说“不要看注释,因为它没有必要”。好吧,Spring是我的容器,它本身就会查看注释并配置所需的内容。然后我不需要我的服务器这样做。
毕竟,我的配置如下所示:
的web.xml:
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="true">
Spring的context.xml:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="PronatecPU" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven />
的persistence.xml:
<persistence-unit name="PronatecPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/pronatec</non-jta-data-source>
<!-- ... -->
我注入的entityManager:
@PersistenceContext(type= PersistenceContextType.EXTENDED)
private EntityManager entityManager;
我希望这能帮助那些遇到同样问题的人。