I am trying to understand Spring Transaction concept. As shown below, i've to insert data into two different databases (iSeries and DB2) but our iSeries version doesn't support Two-Phase commit. The requirement is, Transaction should only be committed if both the Inserts are successful else it should rollback.
If i use the Propagation as REQUIRED or REQUIRES_NEW, i get the error "An illegal attempt to commit a one phase capable resource with existing two phase capable resources has occurred".
But if i use NOT_SUPPORTED or SUPPORTS, it works fine (i.e. Transaction rolls back if one of the Insert fails, else it commits if both the Inserts are successful).
My understanding is, if Propagation = SUPPORTS / NOT_SUPPORTED, then Transaction won't even be started in the below scenario. Hence, both the inserts might happen independently in the two different databases and the whole Transaction shouldn't roll back if one of them fails.
But Propagation = SUPPORTS / NOT_SUPPORTED works as per my requirement. Can someone please explain this? Thanks in advance.
@Resource
private SessionFactory db2SessionFactory = null;
@Resource
private SessionFactory iSeriesSessionFactory = null;
@Transactional(propagation = Propagation.REQUIRED)
public void insert()
{
insertDB1();
insertDB2();
}
insertDB1()
{
db2SessionFactory .getCurrentSession().saveOrUpdate(obj1);
}
insertDB2()
{
iSeriesSessionFactory.getCurrentSession().saveOrUpdate(obj2);
}