Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Having issues with transaction in a pojo set up..

Can someone please help .. I am not able to get a consistent
test/production environment .. If we inject the EntityManager it works
in one environment (test-pojo) but not in the other
environment(production-server) and if inject the EntityManagerFactory
it works in server but not in test..

Cheers
Vaidya


On Tue, Jul 17, 2012 at 4:57 PM, vaidya nathan <vaidyaatdst@xxxxxxxxx> wrote:
> Thanx Michael for the reply.
>
> We did try with the @PersistenceContext EM and it works in a spring
> environment(non jta ). But the issue is that in production we are
> running in a JTA environment (in java ee with ejbs and spring ) and if
> i inject the Entity Managers through the @PersistenceContext EM it
> doesn't work . The issue is that I keep getting an EntityManager each
> time. Please refer to this thread
> http://dev.eclipse.org/mhonarc/lists/eclipselink-users/msg07244.html
>
> for the full description , With injecting the PersistenceUnit EMF it
> is working in a container but it is not working in a stand alone
> spring test environment.
> Cheers
> Vaidya
>
>
>
> On Tue, Jul 17, 2012 at 4:44 PM, Michael O'Brien
> <Michael.F.Obrien@xxxxxxxxx> wrote:
>> Vaidya,
>>         You should be able to DI an @PersistenceContext EM instead of an @PersistenceUnit EMF - this works in the catalina only Tomcat 7 web container.
>>         I have the same spring configuration as yourself except that I use an EM instead of an EMF - here is a spring-test enabled unit test.
>>
>> @RunWith(SpringJUnit4ClassRunner.class)
>> @ContextConfiguration(locations = {"classpath:resources/unittest-server-spring-platform-vigilance-derby-config.xml" }) // overrides setApplicationContext()
>> @TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
>> @Transactional
>> //public abstract class SpringDAOAbstractTest<T>  { // No extends interface will require a dynamic proxy via CGLIB and ASM
>> public abstract class SpringDAOAbstractTest<T>  implements ApplicationContextAware {
>>         /** The following set function is only required when we implement ApplicationContextAware - otherwise use Spring-Test DI */
>>         @Override
>>         public void setApplicationContext(ApplicationContext arg0) throws BeansException {      }
>>
>>         @Autowired
>>         private VigilanceDAOFacadeExport facade;
>>
>> And my DAO
>> @Repository
>> public abstract class VigilanceDAOFacadeAbstractImpl implements VigilanceDAOFacade {..}
>> @Repository
>> public class VigilanceDAOFacadeExportImpl extends VigilanceDAOFacadeAbstractImpl implements VigilanceDAOFacadeExport {
>>     @PersistenceContext(unitName="vigilanceClient")
>>     private EntityManager entityManager;
>>
>>     @Transactional
>>     public List<VigilanceDispensableGeneric> getDispensableGenerics(Locale locale, boolean isEager) {
>>         CriteriaQuery<VigilanceDispensableGeneric> criteriaQuery = getCriteriaBuilder().createQuery(VigilanceDispensableGeneric.class);
>> ...}
>> }
>>
>>>my spring config xml does not override TX levels like your aspects though
>>
>>     <context:annotation-config />
>>
>>     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
>>         p:dataSource-ref="dataSource"
>>         p:entityManagerFactory-ref="entityManagerFactory" />
>>
>>     <tx:annotation-driven />
>>
>>>I also defer to spring to manage the tx within the test (by using @Test alone) and dao methods (like an EE session bean) with no issues unless I use @NotTransactional in which case my criteria queries need to resolve lazy mappings via fetch joins.
>>
>> /Michael
>>
>>
>>
>> -----Original Message-----
>> From: eclipselink-users-bounces@xxxxxxxxxxx [mailto:eclipselink-users-bounces@xxxxxxxxxxx] On Behalf Of vaidya nathan
>> Sent: Tuesday, July 17, 2012 4:31 PM
>> To: EclipseLink User Discussions
>> Subject: [eclipselink-users] Having issues with transaction in a pojo set up..
>>
>> Hi Eclipselink users,
>>
>> Am having an issue with using eclipselink/spring in a non container
>> pojo environment. Following is my setup . I have a DAO controlled by
>> spring which uses the eclipselink entity manager . I inject the entity
>> manager factory using the PersistenceUnit annotation (I had some
>> issues using the EntityManager injected in a jta environment ), so
>> want to inject the EntityManager Factory which according to the JPA
>> spec is ok.. When I try to persist I am getting the following
>> exception
>>
>>
>> com.foo.foobar.common.dao.DAOException:
>> Exception Description: No transaction is currently active
>>         at com.foo.foobar.cm.common.dao.eclipselink.EclipseLinkDAO.updateDataItem(BaseDAO.java:114)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>         at java.lang.reflect.Method.invoke(Method.java:597)
>>         at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
>>         at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
>>         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
>>         at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
>>         at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
>>         at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
>>         at $Proxy29.updateStatus(Unknown Source)
>>
>> Following is my spring config..
>>
>>         <context:annotation-config />
>>         <context:component-scan
>> base-package="com.foo.foobar.cm.common.dao.eclipselink" />
>>         <bean id="entityManagerFactory"
>>                 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>>                 <property name="persistenceUnitName" value="PersistenceEl" />
>>                 <property name="jpaDialect" ref="eclipseLinkDialect" />
>>                 <property name="persistenceXmlLocation"
>>                         value="classpath:META-INF/persistence-local-eclipse.xml" />
>>                 <property name="dataSource" ref="dataSource" />
>>                 <property name="jpaVendorAdapter">
>>                         <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
>>                                 <property name="showSql" value="true" />
>>                                 <property name="generateDdl" value="false" />
>>                         </bean>
>>                 </property>
>>                 <property name="jpaPropertyMap">
>>                         <map>
>>                                 <entry key="eclipselink.jdbc.native-sql" value="false" />
>>                                 <entry key="eclipselink.weaving" value="false" />
>>                                 <entry key="eclipselink.persistence-context.flush-mode" value="AUTO" />
>>                                 <entry key="eclipselink.logging.logger"
>> value="org.eclipse.persistence.logging.DefaultSessionLog" />
>>                                 <entry key="eclipselink.logging.level" value="FINE" />
>>                                 <entry key="eclipselink.orm.throw.exceptions" value="true" />
>>                                 <entry key="eclipselink.logging.exceptions" value="true" />
>>                         </map>
>>                 </property>
>>         </bean>
>>
>>         <bean id="eclipseLinkDialect"
>>                 class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
>>         <!-- the transactional advice (what 'happens'; see the <aop:advisor/>
>> bean below) -->
>>         <tx:advice id="txAdvice" transaction-manager="transactionManager">
>>                 <!-- the transactional semantics... -->
>>                 <tx:attributes>
>>                         <!-- all methods starting with 'get' are read-only -->
>>                         <tx:method name="testGet*" propagation="SUPPORTS"
>>                                 read-only="true" />
>>                         <!-- other methods use the default transaction settings (see below) -->
>>                         <tx:method name="*" propagation="REQUIRES_NEW" read-only="false" />
>>                 </tx:attributes>
>>         </tx:advice>
>>
>>         <aop:config>
>>                 <aop:pointcut id="serviceOperation"
>>                         expression="execution(* com.foo.foobar.cm.common.dao.*.*(..))" />
>>                 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
>>         </aop:config>
>>
>>         <bean id="transactionManager"
>> class="org.springframework.orm.jpa.JpaTransactionManager">
>>                 <property name="entityManagerFactory" ref="entityManagerFactory" />
>>                 <property name="jpaDialect" ref="eclipseLinkDialect" />
>>                 <property name="nestedTransactionAllowed" value="true" />
>>         </bean>
>>
>>
>>
>> persistence file is as follows
>>
>> <?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="MABPersistenceEl" transaction-type="RESOURCE_LOCAL">
>>         <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
>>         <non-jta-data-source>pmabDataSource</non-jta-data-source>
>>
>> //          bunch of classes  ommited here for brevity
>>
>>       <exclude-unlisted-classes />
>>         </persistence-unit>
>> </persistence>
>>
>>
>>
>>
>> my test is as follows
>>
>> @Test
>> @Transactional(propagation=Propagation.REQUIRES_NEW)
>> @Rollback(false)
>> public void testReferenceDataItems()
>> {
>> // Get inStatus and populate it .. dont set the id.
>>   someStatus = (Foo) myDAO.updateStatus(inStatus);
>> }
>>
>>
>> and in myDAO i have the following code
>>
>> public abstract BaseDAO
>> {
>>
>>     @PersistenceUnit(unitName="PersistenceEl")
>>     private EntityManagerFactory entityManagerFactory;
>>
>>     public SomeStatus updateDataItem(SomeStatus inStatus)
>>     {
>>        EntityManager em=entityManagerFactory.createEntityManager();
>>        //check for id,, assume null (not shown for brevity)
>>        em.persist(inStatus);
>>        em.flush(); // it craps out here..
>>         SomeStatus t1= (T)emgr.find(SomeStatus.class,inStatus.getId()); //
>>         return t1;
>>     }
>> }
>>
>>
>>
>> When I had a similiar issue in hibernate (we are involving in
>> converting from hibernate to eclipselink , fyi) , we used the
>> following extra property in the jpa property map
>>         <entry key="hibernate.current_session_context_class"
>> value="org.springframework.orm.hibernate3.SpringSessionContext" />
>> and it started working.
>>
>> Can some one please help me.. ? We are totally struck because of this
>> issue .Is there an equivalent eclipselink specific property that i
>> need to add . Has anyone tried this approach . When we tried the
>> entity Manager it works in the above set up , but it is not working in
>> a jta setup as the Spring Shared EntityManager Creator creates a new
>> entity manager everytime, so we would like to inject the
>> EntityManagerFactory itself...
>>
>> Cheers
>> Vaidya
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top