Thank you Patrick.
At the end I found out it's only my configuration problem. At the my Spring
configuration, the context for the Spring MVC dispatcher servlet has a
component-scan that is duplicated
So the problem occur only when the application is deployed to the JEE server
and I mistakenly think it's a problem when run in JBoss. To my
understanding, the repository, which has the @PersistenceContext
EntityManager injection, was scanned and constructed by Spring in my root
context and also the request context. (don't know why there is no error
about it) For unknown reason under this (incorrect) setting, the Entity
Manager will create new closed Query.
The problem was fixed by adding an exclude-filter as follows:
<context:component-scan base-package="mypackage">
<context:include-filter type="regex"
expression="MY_INCL_EXPRESSION"/>
<context:exclude-filter type="annotation" expression="
org.springframework.stereotype.Repository"/>
</context:component-scan>
re. logging, I suppose I've enabled all log. I set the openjpa.Log parameter
to "log4j" and enabled log4j appender for all "openjpa.*" logger at trace
level. (details are attached at the bottom for reference. let me know if
there are more logging then that, i actually can see all trace log)
So from the log, as I quoted in my first email, it just say it created a new
shared EntityManager, and then any Query created by that new EM is not
opened by default. Anyway, it's just my spring configuration error. Thanks
for your help.
Regards,
mingfai
Reference - logging configuration:
<!-- JPA -->
<bean id="MY_EM_FACTORY"
class="
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="MY_DATASOURCE" />
<property name="persistenceUnitName" value="MY_PU_NAME"/>
<property name="jpaVendorAdapter"><!-- MY ADAPTER BEAN
--></property>
<property name="jpaProperties">
<props>
<prop key="openjpa.Log">log4j</prop>
<prop key="openjpa.ConnectionFactoryProperties">
PrettyPrint=true, PrettyPrintLineLength=72
</prop>
</props>
</property>
</bean>
And in log4j.xml:
<category name="org.apache.openjpa">
<!-- this one is redundant, all logs actually goes to openjpa.* -->
<priority value="TRACE"/>
<appender-ref ref="ORM_LOG"/>
</category>
<category name="openjpa">
<priority value="TRACE"/>
<appender-ref ref="ORM_LOG"/>
</category>
On 1/8/08, Patrick Linskey <[EMAIL PROTECTED]> wrote:
>
> 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
>