views:

198

answers:

1

Is there a way to know if a transaction is in an "ongoing" state in JDBC? I found nothing in the Connection API.

Thanks

+3  A: 

JDBC does not track the transaction state. It is the job of DB to track the transaction state.

Given that, you still have two ways on tracking/knowing the transaction states.

You can make a sql call to your db to ask for transaction specific detail. for oracle, it will be in v$transaction table in suggested in this post.

SELECT COUNT(*) 
  FROM v$transaction t, v$session s, v$mystat 
  WHERE t.ses_addr = s.saddr AND s.sid = m.sid AND ROWNUM = 1;

Another solution is to use transaction manager code in some common frameworks, such as hibernate (I believe Spring has it too).

public interface Session {
  public abstract org.hibernate.Transaction getTransaction();
}

public Transaction {
  public abstract boolean wasRolledBack() throws org.hibernate.HibernateException;

  public abstract boolean wasCommitted() throws org.hibernate.HibernateException;

  public abstract boolean isActive() throws org.hibernate.HibernateException;
}
Oscar Chan
Thanks for the oracle tip, it will help me.
cadrian