Discussion:
CMP with OpenJPA on Apache Geronimo 3.0.1
ivan frias
2014-12-29 15:35:07 UTC
Permalink
Hi,
Currently I am developing an application using OpenJPA . I've decide to use
CMP to manage the transactions.
I am able to insert /edit and delete rows from the database ( through a
configured Datasource ), however, it seems that transaction is only
commited at Geronimo level. If I use the datasource control to execute a
query ( inside the management window on Geronimo ) I get the modified rows,
however If I try it outside the container ( e.g. using Sql Developer ) I
can't see the modifications.

Persistence.xml :


<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>

<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>


TestDaoImpl.java:


package de.carmedialab.db.dao;

import java.util.Calendar;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import de.carmedialab.db.entities.ApplicationItem;
import de.carmedialab.db.entities.ItemType;

@Stateless
@Remote(TestDao.class)
public class TestDaoImpl implements TestDao{
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em;

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save() {

try{
Query query = em.createQuery("select t from ItemType t where t.ittTypeName
= :name");
query.setParameter("name", "Passenger car");
ItemType type = (ItemType)query.getSingleResult();
ApplicationItem itm = new ApplicationItem();
itm.setItemType(type);
itm.setItmIsActive("Y");
itm.setItmItemIdentifier("TEST");
itm.setItmItemDescription("DESCRIPTION");
itm.setItmItemName("NAME");
itm.setItmInsertDate(Calendar.getInstance().getTime());
itm.setItmInsertUser("SYSTEM");
em.persist(itm);
System.out.println("Item Persisted");
}catch(Exception ex){
ex.printStackTrace();
}
}

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete() {
try{
Query query = em.createQuery("select i from ApplicationItem i where
i.itmItemName = :itmName");
query.setParameter("itmName", "NAME");
ApplicationItem itm = (ApplicationItem)query.getSingleResult();
em.remove(itm);
System.out.println("Item Deleted");
}catch(Exception ex){
ex.printStackTrace();
}
}
}


Best regards,
Ivan Frias
David Jencks
2014-12-29 16:03:44 UTC
Permalink
It's been a few years….. IIRC the problem here is that your datasource is not set up correctly. Again IIRC you need both a transactional and a non-transactional datasource for the persistence-unit. Again IIRC this is more likely to work if the transactional datasource is XA. The non transactional datasource must be no-transaction.

hope this helps
david jencks
Hi,
Currently I am developing an application using OpenJPA . I've decide to use CMP to manage the transactions.
I am able to insert /edit and delete rows from the database ( through a configured Datasource ), however, it seems that transaction is only commited at Geronimo level. If I use the datasource control to execute a query ( inside the management window on Geronimo ) I get the modified rows, however If I try it outside the container ( e.g. using Sql Developer ) I can't see the modifications.
<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="validate" />
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" />-->
</properties>
</persistence-unit>
package de.carmedialab.db.dao;
import java.util.Calendar;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import de.carmedialab.db.entities.ApplicationItem;
import de.carmedialab.db.entities.ItemType;
@Stateless
@Remote(TestDao.class)
public class TestDaoImpl implements TestDao{
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save() {
try{
Query query = em.createQuery("select t from ItemType t where t.ittTypeName = :name");
query.setParameter("name", "Passenger car");
ItemType type = (ItemType)query.getSingleResult();
ApplicationItem itm = new ApplicationItem();
itm.setItemType(type);
itm.setItmIsActive("Y");
itm.setItmItemIdentifier("TEST");
itm.setItmItemDescription("DESCRIPTION");
itm.setItmItemName("NAME");
itm.setItmInsertDate(Calendar.getInstance().getTime());
itm.setItmInsertUser("SYSTEM");
em.persist(itm);
System.out.println("Item Persisted");
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete() {
try{
Query query = em.createQuery("select i from ApplicationItem i where i.itmItemName = :itmName");
query.setParameter("itmName", "NAME");
ApplicationItem itm = (ApplicationItem)query.getSingleResult();
em.remove(itm);
System.out.println("Item Deleted");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Best regards,
Ivan Frias
ivan frias
2014-12-29 16:20:21 UTC
Permalink
Hi,

Do you have any example of the steps involved in correctly configuring a
datasource for this case ?

Best regards,
Ivan Frias
It's been a few years
.. IIRC the problem here is that your datasource is
not set up correctly. Again IIRC you need both a transactional and a
non-transactional datasource for the persistence-unit. Again IIRC this is
more likely to work if the transactional datasource is XA. The non
transactional datasource must be no-transaction.
hope this helps
david jencks
Post by ivan frias
Hi,
Currently I am developing an application using OpenJPA . I've decide to
use CMP to manage the transactions.
Post by ivan frias
I am able to insert /edit and delete rows from the database ( through a
configured Datasource ), however, it seems that transaction is only
commited at Geronimo level. If I use the datasource control to execute a
query ( inside the management window on Geronimo ) I get the modified rows,
however If I try it outside the container ( e.g. using Sql Developer ) I
can't see the modifications.
Post by ivan frias
<persistence-unit name="FleaCircus" transaction-type="JTA">
<description>Flea Circus</description>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
Post by ivan frias
<jta-data-source>FleaCircusOracleDS</jta-data-source>
<class>de.carmedialab.db.entities.ApplicationItem</class>
<class>de.carmedialab.db.entities.FleaResult</class>
<class>de.carmedialab.db.entities.FleaResultType</class>
<class>de.carmedialab.db.entities.ItemAttribute</class>
<class>de.carmedialab.db.entities.ItemGroup</class>
<class>de.carmedialab.db.entities.ItemType</class>
<class>de.carmedialab.db.entities.ItemTypeAttribute</class>
<class>de.carmedialab.db.entities.ItemTypeOperationAttribute</class>
Post by ivan frias
<class>de.carmedialab.db.entities.Operation</class>
<class>de.carmedialab.db.entities.OperationAttribute</class>
Post by ivan frias
<class>de.carmedialab.db.entities.OperationType</class>
<class>de.carmedialab.db.entities.Role</class>
<class>de.carmedialab.db.entities.UserAccount</class>
<class>de.carmedialab.db.entities.Measurement</class>
<class>de.carmedialab.db.entities.MeasurementType</class>
<class>de.carmedialab.db.entities.MeasurementAttribute</class>
<class>de.carmedialab.db.entities.MeasurementAttributeType</class>
Post by ivan frias
<class>de.carmedialab.db.entities.Fleet</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="validate" />
Post by ivan frias
<property
name="openjpa.Compatibility.CheckDatabaseForCascadePersistToDetachedEntity"
Post by ivan frias
value="true" />
<!--<property name="openjpa.Log"
value="DefaultLevel=WARN, Runtime=INFO,
Tool=INFO, SQL=TRACE" />-->
Post by ivan frias
</properties>
</persistence-unit>
package de.carmedialab.db.dao;
import java.util.Calendar;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import de.carmedialab.db.entities.ApplicationItem;
import de.carmedialab.db.entities.ItemType;
@Stateless
@Remote(TestDao.class)
public class TestDaoImpl implements TestDao{
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void save() {
try{
Query query = em.createQuery("select t from
ItemType t where t.ittTypeName = :name");
Post by ivan frias
query.setParameter("name", "Passenger car");
ItemType type = (ItemType)query.getSingleResult();
ApplicationItem itm = new ApplicationItem();
itm.setItemType(type);
itm.setItmIsActive("Y");
itm.setItmItemIdentifier("TEST");
itm.setItmItemDescription("DESCRIPTION");
itm.setItmItemName("NAME");
itm.setItmInsertDate(Calendar.getInstance().getTime());
Post by ivan frias
itm.setItmInsertUser("SYSTEM");
em.persist(itm);
System.out.println("Item Persisted");
}catch(Exception ex){
ex.printStackTrace();
}
}
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void delete() {
try{
Query query = em.createQuery("select i from
ApplicationItem i where i.itmItemName = :itmName");
Post by ivan frias
query.setParameter("itmName", "NAME");
ApplicationItem itm =
(ApplicationItem)query.getSingleResult();
Post by ivan frias
em.remove(itm);
System.out.println("Item Deleted");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Best regards,
Ivan Frias
--
Ivan Frias
Loading...