You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Sudhir K Sethi <ks...@gmail.com> on 2010/04/12 19:56:10 UTC
Problem of partial commit in OpenJPA
Hi,
I am getting cases of partial commit in OpenJPA (1.1.x and 1.2.x). I am
using Weblogic server 10.3 and Oracle 10g. Here is the scenario to get this:
I have 2 classes whose relation is aggregation/composition. Department has
‘n’ number of Employees. Inside my façade I have used code as below:
//import statements
@Stateless(name = "Facade10", mappedName = "FacadeMapped10")
@Interceptors(Interceptor1.class)
public class Facade1Bean implements Facade1 {
@PersistenceContext(unitName = "unit1")
EntityManager em;
public void createDept ( Department aDepartment )
throws AppException {
try {
Employee e1 = new Employee();
e1.setXXX()
aDepartment.addEmployee( e1 );
em.persist( aDepartment );
em.flush();
Department newDept = em.find( Department.class, aDepartment.getDeptId()
);
if ( newDept == null ) {
throw new AppException(
.... );
}
em.refresh( newDept );
//add other employees
Employee e2 = new Employee();
e2.setXXX()
newDept.addEmployee( e2 );
Employee e3 = new Employee();
e3.setXXX()
newDept.addEmployee( e3 );
em.persist( newDept );
em.flush();
em.refresh( newDept );
}
finally {
//clean up code
}
}
//other code in class
}
AppException is codes as:
@ApplicationException(rollback = true)
public class AppException extends Exception {
//costructor and other methods
}
When exception does not have I get all objects in tree saved in DB as
dept1{e1, e2, e3}.
When exception occurs I get partial commit of data in DB in which case the
state of objects is: dept1{e1}. But I want the entire tree to be either
saved or rolled back. Please let me know where I am going wrong? Is there
any special elements needed in persistence.xml so that the transaction in
facade bounds to container?
I am using container managed transaction and my persistence.xml is as
below:
<persistence-unit name=" unit1" transaction-type="JTA">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<jta-data-source>myDataSource</jta-data-source>
….
--
View this message in context: http://n2.nabble.com/Problem-of-partial-commit-in-OpenJPA-tp4891679p4891679.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Problem of partial commit in OpenJPA
Posted by Kevin Sutter <kw...@gmail.com>.
On Tue, Apr 13, 2010 at 2:08 PM, Ravi P Palacherla <
ravi.palacherla@oracle.com> wrote:
>
> Kevin,
>
> In sudhir's code snippet, aDepartment is persisted and
> aDepartment.getName()
> is used in the find.
> I guess aDepartment will be managed after the persist statement and the
> find will always return a non-null object. So, I think that newDept should
> never be null.
>
> Is my understanding incorrect ?
>
Ravi,
Right. I couldn't quite figure out under what conditions a non-null
department would be returned. But, since the Department entity wasn't
provided, maybe the key generation had some anomaly that caused a problem
with the find operation. Not sure. I decided to just focus on the partial
commit problem rather than the app itself... :-)
Related to that though is the final persist() call in the sample app. If
newDept was found, then it is already managed by the persistence context.
And, since it was already an existing entity, it doesn't make any sense to
be performing a persist() operation. This should actually result in an
EntityExistsException. Even a merge() in this case would not be necessary
since the entity is already merged.
So, maybe there are more issues with the app that need to resolved...
Kevin
>
> Regards,
> Ravi.
> --
> View this message in context:
> http://n2.nabble.com/Problem-of-partial-commit-in-OpenJPA-tp4891679p4897872.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
Re: Problem of partial commit in OpenJPA
Posted by Ravi P Palacherla <ra...@oracle.com>.
Sudhir ,
I assume you are using container managed transaction and have default
transaction mode for the stateless session bean.
I am unable to replicate the issue on WLS10.3 (openJPA
openjpa-1.1.0-r422266:657916) and here is the testcase
http://n2.nabble.com/file/n4897872/EJBException.zip EJBException.zip that I
used.
To throw AppExcetion after the find I have to do the following:
em.persist( aDepartment );
em.flush();
Department newDept = em.find( Department.class,
aDepartment.getName()+"abc");
if ( newDept == null ) {
throw new AppException(new Exception("dept not found"));
}
With above testcase I am unable to replicate the issue meaning nothing is
persisted in the database in case of AppException. Hence the rollback is
happening completely (not partial).
Please review the attached testcase and let me know how it is different with
your application.
That will help to isolate your problem.
Kevin,
In sudhir's code snippet, aDepartment is persisted and aDepartment.getName()
is used in the find.
I guess aDepartment will be managed after the persist statement and the
find will always return a non-null object. So, I think that newDept should
never be null.
Is my understanding incorrect ?
Regards,
Ravi.
--
View this message in context: http://n2.nabble.com/Problem-of-partial-commit-in-OpenJPA-tp4891679p4897872.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Problem of partial commit in OpenJPA
Posted by Kevin Sutter <kw...@gmail.com>.
I'm not a WebLogic expert, but it looks like the AppException (with rollback
action) is not being processed correctly by the WebLogic container. It
looks like your exception is declared properly to have the "rollback"
action. And, if your app is throwing this exception right after the find on
the Department.class, then the tran should be rolled back and nothing should
be committed to the database. From the information provided, it looks like
OpenJPA is processing as expected and you need to follow up with the
WebLogic App Server folks.
Kevin
On Mon, Apr 12, 2010 at 12:56 PM, Sudhir K Sethi <ks...@gmail.com>wrote:
>
> Hi,
>
> I am getting cases of partial commit in OpenJPA (1.1.x and 1.2.x). I am
> using Weblogic server 10.3 and Oracle 10g. Here is the scenario to get
> this:
>
> I have 2 classes whose relation is aggregation/composition. Department has
> ‘n’ number of Employees. Inside my façade I have used code as below:
>
>
> //import statements
>
> @Stateless(name = "Facade10", mappedName = "FacadeMapped10")
> @Interceptors(Interceptor1.class)
> public class Facade1Bean implements Facade1 {
>
> @PersistenceContext(unitName = "unit1")
> EntityManager em;
>
>
> public void createDept ( Department aDepartment )
> throws AppException {
>
> try {
>
>
> Employee e1 = new Employee();
> e1.setXXX()
>
> aDepartment.addEmployee( e1 );
>
> em.persist( aDepartment );
> em.flush();
>
> Department newDept = em.find( Department.class,
> aDepartment.getDeptId()
> );
> if ( newDept == null ) {
> throw new AppException(
> .... );
> }
> em.refresh( newDept );
>
> //add other employees
> Employee e2 = new Employee();
> e2.setXXX()
>
> newDept.addEmployee( e2 );
>
> Employee e3 = new Employee();
> e3.setXXX()
>
> newDept.addEmployee( e3 );
>
>
> em.persist( newDept );
> em.flush();
> em.refresh( newDept );
>
>
> }
> finally {
> //clean up code
> }
>
> }
> //other code in class
>
> }
>
> AppException is codes as:
>
> @ApplicationException(rollback = true)
> public class AppException extends Exception {
> //costructor and other methods
>
> }
>
>
> When exception does not have I get all objects in tree saved in DB as
> dept1{e1, e2, e3}.
>
> When exception occurs I get partial commit of data in DB in which case the
> state of objects is: dept1{e1}. But I want the entire tree to be either
> saved or rolled back. Please let me know where I am going wrong? Is there
> any special elements needed in persistence.xml so that the transaction in
> facade bounds to container?
>
> I am using container managed transaction and my persistence.xml is as
> below:
>
> <persistence-unit name=" unit1" transaction-type="JTA">
> <provider>
> org.apache.openjpa.persistence.PersistenceProviderImpl
> </provider>
> <jta-data-source>myDataSource</jta-data-source>
> ….
>
> --
> View this message in context:
> http://n2.nabble.com/Problem-of-partial-commit-in-OpenJPA-tp4891679p4891679.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>