Hi, If you intercept the exception and print it and its failed object out, do you get more stack trace information? OpenJPA should be recording a stack trace when close() is called (maybe only with certain debug flags on); this should be a good clue about where the close is happening.
Generally, I do not believe that it is possible to create a closed Query. So I think that either the Query or its EM must be getting closed somewhere before it's returned to you. -Patrick On Jan 6, 2008 12:05 AM, Ming Fai Ma <[EMAIL PROTECTED]> wrote: > hi, > > I got a strange problem that when using Spring managed transaction with > injected persistence context ( i.e. @PersistenceContext private > EntityManager em; ) when deployed to JBoss (not using the default unified > classloader). By debugging at the OpenJPA code and also from the stacktrace, > a newly created Query object is not opened. So for the first time I call > setParameter, exception is thrown from the assertOpen method of the > BrokerImpl. > > <[email protected]><openjpa-1.0.1-r420667:592145 fatal user error> > org.apache.openjpa.persistence.InvalidStateException: The context has been > closed. The stack trace at which the context was closed is held in the > embedded exception. > FailedObject: java.lang.IllegalStateException > at org.apache.openjpa.kernel.BrokerImpl.assertOpen(BrokerImpl.java:4302) > at org.apache.openjpa.kernel.QueryImpl.assertOpen(QueryImpl.java:1623) > at org.apache.openjpa.kernel.DelegatingQuery.assertOpen( > DelegatingQuery.java:629) > at org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java > :470) > at org.apache.openjpa.persistence.QueryImpl.setParameter(QueryImpl.java > :56) > > > At the following Spring JPA log, the first two lines show my previous > transaction is successful and the EM was closed, and then (after I click > another page that) I trigger a query that create a new EM and then an > exception is thrown. > 2008-01-06 15:12:50,454 DEBUG [ > org.springframework.orm.jpa.JpaTransactionManager] Triggering > afterCompletion synchronization > 2008-01-06 15:12:50,454 DEBUG [ > org.springframework.orm.jpa.JpaTransactionManager] Closing JPA EntityManager > [EMAIL PROTECTED] after transaction > 2008-01-06 15:12:51,860 DEBUG [ > org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler] > Creating new EntityManager for shared EntityManager invocation > > > The case is complex as, > > 1. my code has no exception if I use programmatic transaction instead > of Spring managed transaction. It's just not as elegant as the Spring > managed transaction code. > 2. It is normal for Spring to start a new Entity Manager according to > the Spring doc and also a thread about multi-thread transaction *[1] > 3. The Spring managed transaction code does work outside JBoss. I have > a unit test that use mock objects to simulate the whole > WebApplicationContext and there is no exception. There is exception only > when the application is run inside JBoss under a non-unified classloader. > > I guess it is not a problem with OpenJPA, but an issue with Spring in JBoss. > However, as the exception in the OpenJPA code, I'd like to get some ideas > from this mail list in order to figure out how should I ask in the > Spring/JBoss forum. For example, in what scenario a new Entity Manager may > create Query that is not opened by default? Any idea? > > Thank you in advance. > > Regards, > mingfai > > [1] - > http://forum.springframework.org/showthread.php?t=46402&highlight=%40PersistenceContext > -- Patrick Linskey 202 669 5907
