OpenEntityManagerInViewFilter

Ola,

Tenho uma aplicação que usa Spring 3 no container web, e tenho um modulo EJB 3, ambos rodando no JBOSS 4.2.3, faço a injeção do EJB no spring via Annotation @EJB, até ai tudo bem funciona normal, mas estou tendo o velho problema de lazy, configurei o OpenEntityManagerInViewFilter da seguinte forma:

(WEB)
web.xml

<filter>
	<filter-name>openEntityManagerInViewFilter</filter-name>
	<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>		
</filter>
<filter-mapping>
	<filter-name>openEntityManagerInViewFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

spring.xml


<context:load-time-weaver
		weaver-class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />

	<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/EJBMODULE"/>

	<jee:jndi-lookup id="jbossTransactionManager" jndi-name="java:/TransactionManager"/>
	
	<jee:jndi-lookup id="jbossUserTransaction" jndi-name="UserTransaction"/>	

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="transactionManager" ref="jbossTransactionManager" />			
		<property name="userTransaction" ref="jbossUserTransaction" /> 
	</bean>	

	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	
	<tx:jta-transaction-manager />	
	
	<context:annotation-config />

	<context:component-scan base-package="org.springmvc.spring" />

	<!-- <import resource="security-config.xml"/> -->
	<import resource="spring-mvc.xml" />
	<import resource="spring-webflow.xml" />

(EJB)
persistence.xml

<persistence-unit name="EJBMODULE">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<jta-data-source>java:MySqlDS</jta-data-source>		
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
		</properties>
</persistence-unit>

ERRO:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springmvc/resources/spring-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: ELBMODULE] Unable to build EntityManagerFactory

Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager

Já procurei na net mas não achei algo que resolvesse.

Alguém sabe o que não sei ?

Obrigado.

Resolvi essa parte o erro estava no persistence.xml ficando assim:

<persistence-unit name="EJBMODULE">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<jta-data-source>java:MySqlDS</jta-data-source>
		<class>org.elb.entitys.xyz</class>
		<class>org.elb.entitys.yxz</class>
		<class>org.elb.entitys.zxy</class>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="jboss.entity.manager.factory.jndi.name" value="java:/EJBMODULE"/>
			<property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/>
			<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
		</properties>
	</persistence-unit>

Porem estou com problema de lazy…

ERROR [LazyInitializationException] failed to lazily initialize a collection of role: org.elb.entitys.Estado.cidades, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.elb.entitys.Estado.cidades, no session or session was closed

Este é o problema do opensessioninview, no Spring eu corrigi, mais foi diferente do que vc coloco no seu web.xml
no meu caso fico assim:

<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-classe>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>

E no código java, eu não uso ó método close do hibernate/jpa, o Filter do Spring fica responsável por fazer o close…

No meu arquivo de conf. do hibernate tem duas coisas a mais


                <property name="hibernate.current_session_context_class" value="thread" />  
                <property name="hibernate.autocommit" value="false" />  <!--for transaction-->

Então pelo que eu sei o OpenSessionInViewFilter é para quando se usa a camada de persistência pelo Spring, no caso de EJB toda a parte de persistência fica no EJB.

E uma arquitetura distribuida de onde se pode acessar os EJBs de varias modos. O problema esta em reabrir a sessão pelo OpenEntityManagerInViewFilter para buscar os dados em Lazy, com Spring puro e fácil configurar usando o OpenSessionInViewFilter, mas com EJB3 JPA não é tão simples, ele até acha a referencia do JNDI do entityManagerFactory mas não reabre a sessão.

uso:
Jboss 4.2 -> bibliotecas hibernate , persistence, etc… do Jboss.
Spring 3.0.4.RELEASE -> configurado com anotações
1 modulo EAR
1 modulo WAR

Pelo debug percebi essa mensagem:


03:44:28,641 DEBUG [ConnectionManager] aggressively releasing JDBC connection

03:44:28,641 DEBUG [ManagedEntityManagerFactory] ************** closing entity managersession **************

03:44:39,550 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: org.ejbmodule.entitys.Estado.cidades, no session or session was closed