Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transaction state in JDBC

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

Thanks

like image 769
cadrian Avatar asked Oct 15 '22 13:10

cadrian


1 Answers

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;
}
like image 159
Oscar Chan Avatar answered Oct 20 '22 14:10

Oscar Chan