Wednesday, February 11, 2009

JPA: Attempted to deploy PersistenceUnit while being in the wrong state

For a while I would see the following exception at random times in logs while using JPA in Spring.:

Could not open JPA EntityManager for transaction;
nested exception is javax.persistence.PersistenceException:
Exception [TOPLINK-28013] (Oracle TopLink Essentials - 2.1 (Build 60 (11/17/2008))):
Exception Description: Attempted to deploy PersistenceUnit [harborPU]
while being in the wrong state [Undeployed].
Close all factories for this PersistenceUnit.
Turns out the
PersistenceUnit would be left in the wrong state if the EntityManagerFactory in the JpaTemplate has not been closed. I assumed Spring would take care of this for me, but the behaviour wasn't consistent. In my DAO, I tried adding an explicit call to getJpaTemplate().getEntityManagerFactory().close(). Of course that didn't work because the method where I put the call was within the transaction, leading to:
Attempting to execute an operation on a closed EntityManager.;
nested exception is java.lang.IllegalStateException:
Attempting to execute an operation on a closed EntityManager.
Therefore, I had to call the method from within the class using the DAO that made the call to the method involved in the transaction.

I wonder if this has something to do with my transaction setup:

<bean id="entityManagerFactory" 
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="true" />
<!-- set to true if you want the vendor to create the database for you on startup -->
<property name="generateDdl" value="false" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>

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

<tx:advice id="txAdvice">
<tx:method name="get*" propagation="REQUIRED"/>

<!-- interface to my DAO
<aop:advisor pointcut="execution(* *..MyDaoService.*(..))" advice-ref="txAdvice"/>
If you have any insight, please leave a comment :).

1 comment:

  1. Terrific blog to examine it changed into so useful to us we're looking from long term thanks for sharing this weblog..............each one of a kind related doubts approximately oracle get extremely good oracle on-line education institute facts from this oracle fusion hcm on-line trainings
    oracle cloud fusion financials