openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sahoo <Sa...@Sun.COM>
Subject Re: Using OpenJPA within an "old" J2EE1.4 container with managed transactions?
Date Wed, 27 Jun 2007 11:59:19 GMT
Hi Sreedhar,

I think there is a misunderstanding about "container managed 
transaction(CMT)." In a pure Java EE (true for all previous J2EE 
containers as well), only *EJBs* are allowed to use container managed 
transaction feature. It saves the EJB programmer from marking 
transaction boundaries explicitly; each business method is implicitly 
used as a transaction boundary. There is no such facility for non-EJB 
components in a Java EE container. So, a programmer has to specify the 
transaction boundary explicitly. *javax.transaction.UserTransaction*[1], 
which is part of JTA, is the API that is typically used to do this. 
Since Java Persistence API can be used in environment where JTA may not 
be supported, it also supports an alternative API, called 
*EntityTransaction*[2], to control transactions. This API does not have 
to be used in a container environment which supports JTA. /In fact, the 
method EntityManager.getTransaction() which gives you access to the 
EntityTransaction object should not be even called for an EntityManager 
with transaction-type JTA [3]/. By now it should be clear as to why that 
exception was thrown by OpenJPA. As Marc suggested, you should *stop* 
using getTransaction() as  you have configured persistence.xml to JTA 
transaction-type.

There is another concept that can play a role here for a JTA 
transaction-type entity manager. There is another classification of 
entity managers. Depending on how an entity manager is obtained in code, 
it can be either be *application managed* or *container managed* entity 
manager. I see the subject line saying "J2EE1.4", so I am assuming that 
you can't use container managed entity manager. It is very likely that 
you are using *EntityManagerFactory.createEntityManager()* to get hold 
of an entity manager. Let us know if you are not using such an API. Such 
an entity manager is of type application managed. Such an entity manager 
does not *automatically* participate in a transaction which begins 
*after* the entity manager is created. One needs to call 
*EntityManager.joinTransaction()*. You may *not* be doing this in your 
code. Please check this.

I don't know your code well, so I don't know if you are using 
EntityManager in an EJB with container managed transaction or not. If 
yes, see if you need to use joinTransaction(). If *not*, then you should 
code something like this:

//Get hold of UserTransaction object. In Java EE 5, you can use 
injection to avoid JNDI lookup. See [4]
UserTransaction utx = UserTransaction.class.cast(new 
InitialContext().lookup("java:comp/TransactionManager"));
utx.begin();
entityManager.joinTransaction();
entityManager.persist...
utx.commit();

If this does not help, you have to provide more specific code that shows 
how you create and use EntityManager.

Thanks,
Sahoo

[1] 
http://java.sun.com/javaee/5/docs/api/javax/transaction/UserTransaction.html
[2] 
http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityTransaction.html
[3] 
http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#getTransaction()
[4] 
http://weblogs.java.net/blog/ss141213/examples/blog1/web-app1/src/example/RegistrationServlet.java

Sreedhar.sirigiri wrote:
> Hello Patrick,
>
> When I specify the following properties in persistence.xml, I get the
> following exception. 
> "You cannot access the EntityTransaction when using managed transactions"
>
> persistence.xml
> <persistence-unit name="audit" transaction-type="JTA">
>     	<provider>
>             org.apache.openjpa.persistence.PersistenceProviderImpl
>         </provider>
>         
> 	<jta-data-source>java:/AuditDS</jta-data-source>       
>
>         <class>com.vormetric.server.dao.audit.MessageDTO</class>
>         <properties>
> 	        <property name="openjpa.ConnectionURL" value="jdbc:db2:LOGDB"/>
> 		<property name="openjpa.ConnectionProperties"
> value="DatabaseName=LOGDB"></property>
>  		<property name="openjpa.ConnectionDriverName"
> value="COM.ibm.db2.jdbc.DB2DataSource"/>
>  		<property name="openjpa.ConnectionFactoryMode"  value="managed"/>
> 		<property name="openjpa.TransactionMode" value="managed"/>
> 		<property name="openjpa.ManagedRuntime"
> value="jndi(TransactionManagerName=java:/TransactionManager)"/>
> 		
> 		<!-- TODO: Commented out TRACE levels to reduce level of logs -->
> <!--		<property name="openjpa.Log" value="DefaultLevel=TRACE,SQL=TRACE" /> 
> --> 
> 		<property name="openjpa.RetryClassRegistration" value="true"/>			
> 		<property name="openjpa.jdbc.DBDictionary" value="db2"/>
> 	</properties>
> </persistence-unit>
>
> In my DAOImpl.java I'm explicitly handling transactions by calling
> session.getTransaction().begin() and session.getTransaction().commit(); 
> When the trasaction-type is JTA and TransactionMode="managed" why do we need
> to handle the transactions programatically?? Do I need to do something else
> for the container to handle transactions?
>
> When I modify the TransactionMode="local" and call the begin & commit
> methods, I'm able to update/insert/delete. Kindly help. How can we use
> container managed transactions in OpenJPA. I'm not using any EJB's in my
> project.
>
> Sreedhar
>
>
>
>
> Patrick Linskey wrote:
>   
>> Looking at the trace, it looks like OpenJPA is being deployed correctly
>> when you used java:comp/UserTransaction:
>>
>>     
>>> 156  INFO   [RMI TCP Connection(7)-192.168.0.6] openjpa.Runtime -
>>> Starting OpenJPA 0.9.6-incubating
>>>       
>> This means that OpenJPA loaded the configuration and initialized.
>>
>>     
>>> 359  INFO   [RMI TCP Connection(7)-192.168.0.6] openjpa.jdbc.JDBC - 
>>> OpenJPA will now connect to the database to attempt to determine 
>>> what type of database dictionary to use.  To prevent this connection 
>>> in the future, set your openjpa.jdbc.DBDictionary configuration 
>>> property to the appropriate value for your database (see the 
>>> documentation for available values).
>>> 469  INFO   [RMI TCP Connection(7)-192.168.0.6] openjpa.jdbc.JDBC - 
>>> Using dictionary class "org.apache.openjpa.jdbc.sql.MySQLDictionary" 
>>> (MySQL 5.0.27-community-nt ,MySQL-AB JDBC Driver mysql-connector-
>>> java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, 
>>> $Revision: 5908 $ )).
>>>       
>> This means that we were able to connect to the database and figure out
>> that you're using MySQL.
>>
>>     
>>> org.apache.openjpa.persistence.InvalidStateException:
>>> You cannot access the EntityTransaction when using managed transactions.
>>>        at
>>> org.apache.openjpa.persistence.EntityManagerImpl.getTransaction(EntityManagerImpl.java:360)
>>>        at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.initializeJPA(IMSSystemBean.java:1102)
>>>        at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.startup(IMSSystemBean.java:1061)
>>>        at
>>> org.objectweb.jonas_gen.com.lbslogics.ims.system.interfaces.JOnASIMSSystemBean2005147373Remote.startup(JOnASIMSSystemBean2005147373Remote.java:23
>>>       
>> 4)
>>
>> It looks like IMSSystemBean.initializeJPA() is trying to invoked
>> EntityManager.getTransaction(). Since you're using JTA, you're not allowed
>> to use getTransaction(); all transaction management must happen through
>> container-managed transactions or bean-managed transaction code.
>>
>> -Patrick
>>
>> -- 
>> Patrick Linskey
>> BEA Systems, Inc. 
>>
>> _______________________________________________________________________
>> Notice:  This email message, together with any attachments, may contain
>> information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
>> entities,  that may be confidential,  proprietary,  copyrighted  and/or
>> legally privileged, and is intended solely for the use of the individual
>> or entity named in this message. If you are not the intended recipient,
>> and have received this message in error, please immediately return this
>> by email and then delete it. 
>>
>>     
>>> -----Original Message-----
>>> From: Hans J. Prueller [mailto:hans.prueller@gmx.net] 
>>> Sent: Monday, February 05, 2007 12:54 PM
>>> To: open-jpa-dev@incubator.apache.org
>>> Subject: AW: Using OpenJPA within an "old" J2EE1.4 container 
>>> with managed transactions?
>>>
>>>       
>>>>> If that doesn't work, can you post the complete stack trace?
>>>>>           
>>> Thank you for your help. Unfortunately it did NOT work. I'm 
>>> not sure what
>>> the exact problem is, here are the strack-traces:
>>>
>>> first case: 
>>> ====================================================================
>>> openjpa.ManagedRuntime:
>>> jndi(TransactionManagerName=java:comp/UserTransaction)
>>>
>>> 2007-02-05 21:33:32,109 : IMSSystemBean.initializeJPA : 
>>> initializing JPA
>>> persist
>>> ence.
>>> 2007-02-05 21:33:33,796 : IMSSystemBean.initializeJPA : testing JPA
>>> persistence
>>> 156  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.Runtime - Starting
>>> OpenJ
>>> PA 0.9.6-incubating
>>> 359  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.jdbc.JDBC - OpenJPA
>>> will
>>>  now connect to the database to attempt to determine what 
>>> type of database
>>> dicti
>>> onary to use.  To prevent this connection in the future, set your
>>> openjpa.jdbc.D
>>> BDictionary configuration property to the appropriate value for your
>>> database (s
>>> ee the documentation for available values).
>>> 469  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.jdbc.JDBC - Using
>>> dictio
>>> nary class "org.apache.openjpa.jdbc.sql.MySQLDictionary" (MySQL
>>> 5.0.27-community
>>> -nt ,MySQL-AB JDBC Driver mysql-connector-java-5.0.4 ( $Date: 
>>> 2006-10-19
>>> 17:47:4
>>> 8 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ )).
>>> 812  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.MetaData - Found 1
>>> class
>>> es with metadata in 15 milliseconds.
>>> 844  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.MetaData - Found 1
>>> class
>>> es with metadata in 0 milliseconds.
>>> 1094  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.MetaData - Parsing
>>> clas
>>> s "com.lbslogics.ims.util.JPATestObject".
>>> 1094  INFO   [RMI TCP Connection(7)-192.168.0.6] 
>>> openjpa.MetaData - Parsing
>>> pack
>>> age "com.lbslogics.ims.util.JPATestObject".
>>> 1359  INFO   [RMI TCP Connection(7)-192.168.0.6] openjpa.jdbc.Schema -
>>> Reading t
>>> able information for schema name "null", table name "JPATestObject".
>>> 1437  INFO   [RMI TCP Connection(7)-192.168.0.6] openjpa.jdbc.Schema -
>>> Reading s
>>> equence information for schema "null", sequence name "null".
>>> <4|false|0.9.6-incubating>
>>> org.apache.openjpa.persistence.InvalidStateException:
>>>  You cannot access the EntityTransaction when using managed 
>>> transactions.
>>>         at
>>> org.apache.openjpa.persistence.EntityManagerImpl.getTransaction(Entit
>>> yManagerImpl.java:360)
>>>         at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.initializeJPA(IMSSystemBea
>>> n.java:1102)
>>>         at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.startup(IMSSystemBean.java
>>> :1061)
>>>         at
>>> org.objectweb.jonas_gen.com.lbslogics.ims.system.interfaces.JOnASIMSS
>>> ystemBean2005147373Remote.startup(JOnASIMSSystemBean2005147373
>>> Remote.java:23
>>> 4)
>>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>         at
>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
>>> java:39)
>>>         at
>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
>>> sorImpl.java:25)
>>>         at java.lang.reflect.Method.invoke(Method.java:585)
>>>         at
>>> sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
>>>         at
>>> org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.dispatch(JUnica
>>> stServerRef.java:143)
>>>         at sun.rmi.transport.Transport$1.run(Transport.java:153)
>>>         at java.security.AccessController.doPrivileged(Native Method)
>>>         at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
>>>         at
>>> sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
>>> 66)
>>>         at
>>> sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
>>> .java:707)
>>>         at java.lang.Thread.run(Thread.java:595)
>>> 2007-02-05 21:33:35,187 : IMSSystemBean.startup : ERROR while 
>>> initializing
>>> JPA:
>>> You cannot access the EntityTransaction when using managed 
>>> transactions.
>>>
>>>
>>> second case: 
>>> ====================================================================
>>> openjpa.ManagedRuntime: jndi(TransactionManagerName=/UserTransaction)
>>>
>>> <0|false|0.9.6-incubating>
>>> org.apache.openjpa.persistence.PersistenceException:
>>> /UserTransaction
>>>         at
>>> org.apache.openjpa.kernel.AbstractBrokerFactory.syncWithManagedTransa
>>> ction(AbstractBrokerFactory.java:633)
>>>         at
>>> org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:292)
>>>         at
>>> org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBro
>>> kerFactory.java:165)
>>>         at
>>> org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(Delegatin
>>> gBrokerFactory.java:139)
>>>         at
>>> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityM
>>> anager(EntityManagerFactoryImpl.java:187)
>>>         at
>>> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityM
>>> anager(EntityManagerFactoryImpl.java:140)
>>>         at
>>> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityM
>>> anager(EntityManagerFactoryImpl.java:52)
>>>         at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.initializeJPA(IMSSystemBea
>>> n.java:1099)
>>>         at
>>> com.lbslogics.ims.system.ejb.IMSSystemBean.startup(IMSSystemBean.java
>>> :1061)
>>>         at
>>> org.objectweb.jonas_gen.com.lbslogics.ims.system.interfaces.JOnASIMSS
>>> ystemBean2005147373Remote.startup(JOnASIMSSystemBean2005147373
>>> Remote.java:23
>>> 4)
>>>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>         at
>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
>>> java:39)
>>>         at
>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
>>> sorImpl.java:25)
>>>         at java.lang.reflect.Method.invoke(Method.java:585)
>>>         at
>>> sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
>>>         at
>>> org.objectweb.carol.rmi.jrmp.server.JUnicastServerRef.dispatch(JUnica
>>> stServerRef.java:143)
>>>         at sun.rmi.transport.Transport$1.run(Transport.java:153)
>>>         at java.security.AccessController.doPrivileged(Native Method)
>>>         at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
>>>         at
>>> sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
>>> 66)
>>>         at
>>> sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
>>> .java:707)
>>>         at java.lang.Thread.run(Thread.java:595)
>>> Caused by: javax.naming.NameNotFoundException: /UserTransaction
>>>         at
>>> com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java
>>> :95)
>>>         at javax.naming.InitialContext.lookup(InitialContext.java:355)
>>>         at
>>> org.objectweb.carol.jndi.spi.AbsContext.lookup(AbsContext.java:140)
>>>         at
>>> org.objectweb.carol.jndi.spi.AbsContext.lookup(AbsContext.java:150)
>>>         at javax.naming.InitialContext.lookup(InitialContext.java:351)
>>>         at
>>> org.objectweb.carol.jndi.spi.MultiContext.lookup(MultiContext.java:11
>>> 8)
>>>         at javax.naming.InitialContext.lookup(InitialContext.java:351)
>>>         at
>>> org.apache.openjpa.ee.JNDIManagedRuntime.getTransactionManager(JNDIMa
>>> nagedRuntime.java:51)
>>>         at
>>> org.apache.openjpa.kernel.AbstractBrokerFactory.syncWithManagedTransa
>>> ction(AbstractBrokerFactory.java:598)
>>>         ... 21 more
>>> 2007-02-05 21:44:10,921 : IMSSystemBean.startup : ERROR while 
>>> initializing
>>> JPA:
>>> /UserTransaction
>>>
>>> *************************************************
>>>
>>>
>>> From my understanding it seems that there is a fatal error in 
>>> the second 
>>> case, like openJPA was not able to even lookup JOnAS' 
>>> transaction manager.
>>> It seems to me that in the first case, the transaction 
>>> manager lookup worked
>>> but there is another subsequent error? 
>>>
>>> <4|false|0.9.6-incubating>
>>> org.apache.openjpa.persistence.InvalidStateException:
>>>  You cannot access the EntityTransaction when using managed 
>>> transactions.
>>>         at
>>> org.apache.openjpa.persistence.EntityManagerImpl.getTransaction(Entit
>>> yManagerImpl.java:360)
>>>
>>> Can you explain what this message means exactly?
>>>
>>> thank you in advance,
>>> HANS
>>>
>>>
>>>       
>>>> -----Urspr√ľngliche Nachricht-----
>>>> Von: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] 
>>>>         
>>> Im Auftrag von
>>>       
>>>> Marc Prud'hommeaux
>>>> Gesendet: Montag, 05. Februar 2007 21:19
>>>> An: open-jpa-dev@incubator.apache.org
>>>> Betreff: Re: Using OpenJPA within an "old" J2EE1.4 
>>>>         
>>> container with managed
>>>       
>>>> transactions?
>>>>
>>>> Hans-
>>>>
>>>> We might not have Jonas' TransactionManager location configured in
>>>> the automatic TM lookup in OpenJPA. Can you try to manually specify
>>>> it with the following property:
>>>>
>>>>     openjpa.ManagedRuntime: jndi(TransactionManagerName=java:comp/
>>>> UserTransaction)
>>>>
>>>> if that doesn't work, try:
>>>>
>>>>     openjpa.ManagedRuntime: jndi(TransactionManagerName=/
>>>> UserTransaction)
>>>>
>>>> (apparently, Jonas' UserTransaction implementation is also their
>>>> TransactionManager implementation)
>>>>
>>>> If that doesn't work, can you post the complete stack trace?
>>>>
>>>> If it does work, please let us know so we can add it to the list of
>>>> auto-discovered transaction managers.
>>>>
>>>>
>>>>
>>>> On Feb 5, 2007, at 12:05 PM, Hans J. Prueller wrote:
>>>>
>>>>         
>>>>> Hi there,
>>>>>
>>>>>
>>>>>
>>>>> I'm trying to migrate our "old" J2EE1.4 / EJB2.1 applications
>>>>> persistence to
>>>>> openJPA. Currently I got stuck when
>>>>>
>>>>> trying to configure OpenJPA persistence, when creating the
>>>>> EntityManagerFactory and the EntityManager instance,
>>>>>
>>>>> I get the following error:
>>>>>
>>>>>
>>>>>
>>>>> <4|true|0.9.6-incubating>
>>>>> org.apache.openjpa.persistence.InvalidStateException:
>>>>>
>>>>> Could not perform automatic lookup of EJB container's
>>>>> javax.transaction.Transact
>>>>>
>>>>> ionManager implementation. Please ensure that you are running the
>>>>> application fr
>>>>>
>>>>> om within an EJB 1.1 compliant EJB container, and then set the
>>>>> org.apache.openjp
>>>>>
>>>>> a.ManagedRuntime property to the appropriate value to obtain the
>>>>> TransactionMana
>>>>>
>>>>> ger.
>>>>>
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.ee.AutomaticManagedRuntime.getTransactionManager(A
>>>       
>>>>> utomaticManagedRuntime.java:180)
>>>>>
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.kernel.AbstractBrokerFactory.syncWithManagedTransa
>>>       
>>>>> ction(AbstractBrokerFactory.java:598)
>>>>>
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:292)
>>>       
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBro
>>>       
>>>>> kerFactory.java:165)
>>>>>
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(Delegatin
>>>       
>>>>> gBrokerFactory.java:139)
>>>>>
>>>>>         at
>>>>>
>>>>>           
>>> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityM
>>>       
>>>>> anager(EntityManagerFactoryImpl.java:187)
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Currently I'm running the application within the JOnAS 4.8.3 J2EE
>>>>> Server.
>>>>> How can I configure open JPA to
>>>>>
>>>>> use the provided managed Transactions of JOnAS? (this 
>>>>>           
>>> works also with
>>>       
>>>>> Hibernate 2.x so I assume it should
>>>>>
>>>>> somehow be possible with openJPA?)
>>>>>
>>>>>
>>>>>
>>>>> any help appreciated!
>>>>>
>>>>>
>>>>>
>>>>> regards,
>>>>>
>>>>> HANS
>>>>>
>>>>>
>>>>>
>>>>> ===========================
>>>>> virtually hanzz...
>>>>>
>>>>>
>>>>>
>>>>>  <http://hanzz.zapto.org> http://hanzz.zapto.org (personal)
>>>>>  <http://www.cse.dmu.ac.uk/~hansp> http://www.cse.dmu.ac.uk/~hansp
>>>>> (research)
>>>>>
>>>>>
>>>>>
>>>>>           
>>>       
>>     
>
>   

Mime
View raw message