What do you see as the merits/faults of the following pattern?
Code:
Session sess = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
... do some work A ...
tx.commit();
... do some work B ...
tx.commit()
... do some work C ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
Is the above equivelant to the following, disregarding the difference that A, B, and C can commit/rollback independently of each other?
Code:
Session sess = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
... do some work A ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
tx = session.beginTransaction();
try {
... do some work B ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
tx = session.beginTransaction();
try {
... do some work C ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
In other words, does it matter whether I use the same transaction object throughout as opposed to getting a new transaction object for each block of work?
What about the following?
Code:
Session sess = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
... do some work A ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
sess.close();
sess = sessionFactory.openSession();
tx = session.beginTransaction();
try {
... do some work B ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
sess.close();
sess = sessionFactory.openSession();
tx = session.beginTransaction();
try {
... do some work C ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
sess.close();
Is there any advantage to having new sessions for each block of work rather than using the same session object throughout? I'm assuming the session object would be configured/managed the same way for each block of work.
Suppose that A and B do inserts and deletes, while C does a query where you expect the inserts and deletes to be factored into the results. Are any of the above patterns more or less suitable for that situation?
One more, a nested transaction.
Code:
Session sess = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
... do some work A ...
Transaction tx2 = session.beginTransaction();
try {
... do some work B ...
tx.commit()
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
... do some work C ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
If B throws an exception and is rolled back, could A and C still commit? Or are tx and tx2 really using the same underlying DB transaction? If so, would I have to get a new session for the inner transaction like the following:
Code:
Session sess = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
... do some work A ...
Session sess2 = sessionFactory.openSession();
Transaction tx2 = session.beginTransaction();
try {
... do some work B ...
tx.commit()
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}
sess2.close();
... do some work C ...
tx.commit();
} catch (HibernateException he) {
if (tx != null) tx.rollback();
}