TX その17 TxManagerとTransactionImpl

その14で書いた

またTxManagerにはTransactionインスタンスのsetterメソッドは無い.従ってThreadLocalにTransactionインスタンスを持ったThreadInfoを突っ込んでおかないといけない.TxManagerをnewして何もしないでgetTransaction()を呼ぶとnullが返る.

これは間違いかも.もう一度ソースを読む必要有り.

public void begin()
     throws NotSupportedException, SystemException
{
  trace = log.isTraceEnabled();
  
  ThreadInfo ti = getThreadInfo();
  TransactionImpl current = ti.tx;

  if (current != null)
  {
     if (current.isDone())
        disassociateThread(ti);
     else
        throw new NotSupportedException
              ("Transaction already active, cannot nest transactions.");
  }

  long timeout = (ti.timeout == 0) ? timeOut : ti.timeout;
  TransactionImpl tx = new TransactionImpl(timeout);
  associateThread(ti, tx);
  localIdTx.put(tx.getLocalId(), tx);
  if (globalIdsEnabled)
     globalIdTx.put(tx.getGlobalId(), tx);

  if (trace)
     log.trace("began tx: " + tx);
}

TransactionImplのソースも読み直す.どうやらTransactionImplインスタンスの再利用をしているらしい.パフォーマンスのためかな?

commitが呼ばれた時にinstanceDoneというメソッドが呼ばれて,TransactionImpl自体が保有しているリソースを開放し,状態がdoneになる.

"current != null"であった場合というのは,例えばEJB1の中でbegin->commitが行われた,そして同じスレッド内で次のEJB2が呼ばれてその中でbeginが呼ばれたみたいな時と思われる.あるいはこのスレッド自体をプーリングしているのかもしれない.

さておき,このbeginメソッドにあるように,TransactionImplインスタンスはTxManagerのbeginメソッドの中でnewされるわけだ.デフォルトのタイムアウトは60秒.