Here is what I observed.

1. If @PersistenceContext(unitName="Tutorial") is used, irrespective of "transaction-type" (JTA or RESOURCE_LOCAL) the ContainerManaged EM is returned. UserTransaction object should be used to perform transactions. Because, if EntityTransaction object is used, the server throws exception. The persistence context is carried over by JTA transaction during the life of the transaction. In this case, if the value "RESOURCE_LOCAL" is used for  "transaction-type" the commits will not happen. I think the value "JTA" for transaction-type attribute only makes sense??

2. if @PeristenceUnit(unitName="Tutorial") is used and the value of transaction-type is RESOURCE_LOCAL, whenever EM is obtained from EMF, a new persistence context is returned. EntityTransaction object should be used to perform transactions. Using UserTransaction Object does not commit the values.

3. if @PeristenceUnit(unitName="Tutorial") is used and the value of transaction-type is JTA, whenever EM is obtained from EMF, a new persistence context is returned. EntityTransaction object can not be used. The server throws exception. Only UserTransaction object can be used. Even though, a new PeristenceContext is obtained every time EM is got from the EMF, the last update made to an entity (irrespective of which PersistenceContext made the update) is committed to the database.

I am confused as to where the above scenarios come into play in real world applications. But I do understand ContainerManaged EM + JTA and ApplicationManaged EM + RESOURCE_LOCAL combinations. Request clarification on other combinations??

Thanks
Phani



On Tue, Jun 10, 2008 at 9:23 AM, Phani Madgula <phanibalaji.madgula@gmail.com> wrote:
Thanks for the info. I looked into Spec and got a very vague idea of what's happening here. Let me test these scenarios with some samples and understand what's happening.

Thanks
Phani


On Mon, Jun 9, 2008 at 11:35 PM, David Jencks <david_jencks@yahoo.com> wrote:
I looked in the spec a bit and am not sure I understand how all the parts are meshing, maybe david blevins can chime in...   I think that you've specified contradictory information and maybe we should be complaining rather than supplying a container managed jta EntityManager.

I _think_ that your annotation-injected EntityManager has to be container managed and hence jta.
I _think_ that if you want a resource-local EntityManager you have to inject the EMF with a PersistenceUnit annotation and get the EntityManager yourself.

In the spec, section 5.6 starts:
Whenacontainer-managedentitymanager isused, thelifecycleof thepersistencecontext isalways 
managedautomatically, transparentlytotheapplication, andthepersistencecontextispropagatedwith 
the JTA transaction. 


I think this means that with your @PersistenceContext annotation, you are definitely going to get a tx-type=JTA EM.  Since you specified RESOURCE_LOCAL in the persistence.xml I think we should perhaps be objecting.

I also think this means that you need an application managed persistence context to get RESOURCE_LOCAL.  There are a bunch of examples in section 5.7 that all use @PersistenceUnit.

Also hoping for clarification....
david jencks

On Jun 9, 2008, at 1:43 AM, Phani Madgula wrote:

Hi,
I have tried to play with <jta-datasource> and <non-jta-datasource> as follows.

I have the following peristence.xml in a web application.
***********
<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
version="1.0">

   <!-- Tutorial "unit" -->
   <persistence-unit name="Tutorial" transaction-type="RESOURCE_LOCAL">
   <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
   <non-jta-data-source>ProductDS-nonJTA</non-jta-data-source>
   <mapping-file>orm.xml</mapping-file>
   <class>samples.jpa.Product</class>
   <class>samples.jpa.Book</class>

   </persistence-unit>

</persistence>

***********

In the servlet, I have the following code

***********
@PersistenceContext(unitName="Tutorial")
  private EntityManager em;
.....
....
UserTransaction ut;
try{
Context ctx = new InitialContext();

//ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
//ut.begin();

//EntityTransaction et = em.getTransaction();
//et.begin();

Book book = new Book("DDDDD","John",1.0,"Joe","1934834-23823","Phani");
em.persist(book);

//et.commit();
//ut.commit();

}catch(Exception e){
e.printStackTrace();
throw new ServletException (e);
}
***********************

1. When I hit the servlet, I get the following error.

  ***********************
  javax.persistence.TransactionRequiredException : No Active transaction
  ***********************

2. Suppose I have the following code in the servlet
   **********************
***********
@PersistenceContext(unitName="Tutorial")
  private EntityManager em;
.....
....
UserTransaction ut;
try{
Context ctx = new InitialContext();

//ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
//ut.begin();

//Uncomment EntityTransaction
EntityTransaction et = em.getTransaction();
et.begin();

Book book = new Book("DDDDD","John",1.0,"Joe","1934834-23823","Phani");
em.persist(book);

       et.commit();
//ut.commit();

}catch(Exception e){
e.printStackTrace();
throw new ServletException (e);
}
 ***********************
 I get the following error
 ***********************

 java.lang.IllegalStateException : You can not call getTransaction on
a container managed EntityManager

 *************************

3. Only if I use JTA API, I am able to get the entity persisted
properly as below.
***************************
@PersistenceContext(unitName="Tutorial")
  private EntityManager em;
.....
....
UserTransaction ut;
try{
Context ctx = new InitialContext();

ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");
ut.begin();


Book book = new Book("DDDDD","John",1.0,"Joe","1934834-23823","Phani");
em.persist(book);


ut.commit();

}catch(Exception e){
e.printStackTrace();
throw new ServletException (e);
}
****************************

Please note that I am using "RESOURCE_LOCAL" persistence unit. I also
used <non-jta-datasource>. I deployed the database pool with
</no-transaction> tag in the connector deployment plan.

My questions are

1. Should we always use UserTransaction object irrespective of
<jta-datasource> or <non-jta-datasource> in JEE environment??

2. How is the use of <non-jta-datsource> different from the use
<jta-datasource> in JEE environment??

3. In EJBs, when ContainerManaged Transactions are used, there is no
need to use JTA. Is that correct??

4. Only in J2SE environments, we can use EntityTransaction object. is
that correct??

Thanks in advance.
Phani

On 5/28/08, David Jencks <david_jencks@yahoo.com> wrote:
1. I hope you named the file openejb-jar.xml
2. jta-datasource and non-jta-datasource have to contain the names of
the datasources in the geronimo plan used for the pool, NOT some jndi
name you might also map them to.  The datasources don't need to be
bound in jndi in order to be used for jpa
3. The non-jta-datasource must be a datasource that really has no
transaction support, using the <no-transaction/> element instead of
<local-transaction/> or <xa-transaction>... in the connector plan.
With derby I find it necessary to have a non-jta-datasource if any ddl
is needed or if openjpa is generating the primary keys.  I don't know
if you can get away without one for other databases.  If you want to
experiment, leave out the non-jta-datasource rather than duplicating
the jta-datasource contents.

hope this helps
david jencks

On May 27, 2008, at 4:09 PM, zeros wrote:


Good evening:

  I'm newbie with EJB3.0. I want to configure the persistence.xml
to have
entities managed with JPA. I have the next configuration files:
OPENJPA-JAR.XML

<openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
<sys:environment>
  <sys:moduleId>
    <sys:groupId>o2o.marketing</sys:groupId>
    <sys:artifactId>EJB</sys:artifactId>
    <sys:version>1.0.8</sys:version>
    <sys:type>jar</sys:type>
  </sys:moduleId>
  <sys:dependencies>
      <sys:dependency>
          <sys:groupId>console.dbpool</sys:groupId>
       <sys:artifactId>marketing</sys:artifactId>
       <sys:version>1.0</sys:version>
         <sys:type>rar</sys:type>
</sys:dependency>
</sys:dependencies>
</sys:environment>
</openejb-jar>

And I have also persistence.xml

<?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="marketing">
   <description>Entity Beans for User</description>

<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</
provider>
   <jta-data-source>java:comp/env/marketing</jta-data-source>
   <non-jta-data-source>java:comp/env/marketing</non-jta-data-source>
      <mapping-file>META-INF/orm.xml</mapping-file>
      <properties />
</persistence-unit>
</persistence>

The error which I'm having is the next one: Unable to resolve
reference
"JtaDataSourceWrapper" and Unable to resolve reference
"NonJtaDataSourceWrapper", which are basically the same error.

I think this is produced because I'm not mapping the Datasocurce to
the
DBPool. Geronimo returns to me examples to do it for web, but no
example to
do it for an EJB.

Please could you help me?

WATCH OUT! I'm talking about entities and EJB3.0, not entiti beans and
EJB2.0

Thanks in advance

SERGIO
--
View this message in context:
http://www.nabble.com/JPA%2C-entities-and-EJB3-tp17502079s134p17502079.html
Sent from the Apache Geronimo - Users mailing list archive at
Nabble.com.