Confused with Transaction Management
shanikaweerapperuma May 20, 2010 8:01 AMHi,
I am new to this topic and bit confused as to what strategy exactly i am using since when i read about it there are so many strategies available. And i am not sure if I am using Seam managed transactions. I am using EJB3, seam, JSF with facelets in my application.
I am injecting my entity manager as @PersistenceContext (type=EXTENDED) to my stateful session bean. Accoding to documentation that means I am using Container Managed Persistance Context and not Seam Managed PC (since i dond't use @In to inject). Also I dont have any transactions attributes set to either to the class level or to the method level. So i think that means I am using Container Managed Transaction demarcation and expect the container to take care of my transactions. Please correct me if i am wrong.
So with this, I am trying to see how to rolback the transaction that fails with an Application Exception. Do i have to call setRollbackOnly method of the EJBContext interface before throwing my Exception? If so how do i get the Context object to call setRollbackOnly method?
In my example below, i need to do a batch process. Either all objects in the ArrayList must get saved to DB or none if either one failes.
@Stateful
@Scope(ScopeType.SESSION)
@Name("WFMaintenance")
public class WFMaintenanceBean extends MyBean implements WFMaintenance
{
@PersistenceContext (type=EXTENDED)
private EntityManager em;
private ArrayList<Version> selectedVersions = new ArrayList<Version>();
public void doBatchProcess(){
try {
for (Version v : selectedVersions)
{
if (v.getItem() == null){
// Should I call setRollbackOnly() here?
throw new Exception("Error :Item of this version is null");
}
// setting new values to v
// ....
em.merge(v);
em.flush();
}
} catch (Exception e) {
// Should I call setRollbackOnly() here as well?
e.printStackTrace();
}
}
...
}
Please help me to clarify this.
Thanks
Shanika
I am new to this topic and bit confused as to what strategy exactly i am using since when i read about it there are so many strategies available. And i am not sure if I am using Seam managed transactions. I am using EJB3, seam, JSF with facelets in my application.
I am injecting my entity manager as @PersistenceContext (type=EXTENDED) to my stateful session bean. Accoding to documentation that means I am using Container Managed Persistance Context and not Seam Managed PC (since i dond't use @In to inject). Also I dont have any transactions attributes set to either to the class level or to the method level. So i think that means I am using Container Managed Transaction demarcation and expect the container to take care of my transactions. Please correct me if i am wrong.
So with this, I am trying to see how to rolback the transaction that fails with an Application Exception. Do i have to call setRollbackOnly method of the EJBContext interface before throwing my Exception? If so how do i get the Context object to call setRollbackOnly method?
In my example below, i need to do a batch process. Either all objects in the ArrayList must get saved to DB or none if either one failes.
@Stateful
@Scope(ScopeType.SESSION)
@Name("WFMaintenance")
public class WFMaintenanceBean extends MyBean implements WFMaintenance
{
@PersistenceContext (type=EXTENDED)
private EntityManager em;
private ArrayList<Version> selectedVersions = new ArrayList<Version>();
public void doBatchProcess(){
try {
for (Version v : selectedVersions)
{
if (v.getItem() == null){
// Should I call setRollbackOnly() here?
throw new Exception("Error :Item of this version is null");
}
// setting new values to v
// ....
em.merge(v);
em.flush();
}
} catch (Exception e) {
// Should I call setRollbackOnly() here as well?
e.printStackTrace();
}
}
...
}
Please help me to clarify this.
Thanks
Shanika