01: /*
02: * XAPool: Open Source XA JDBC Pool
03: * Copyright (C) 2003 Objectweb.org
04: * Initial Developer: Lutris Technologies Inc.
05: * Contact: xapool-public@lists.debian-sf.objectweb.org
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20: * USA
21: */
22: package org.enhydra.jdbc.standard;
23:
24: import java.sql.Connection;
25: import javax.transaction.xa.Xid;
26: import javax.transaction.Status;
27:
28: /**
29: * Provides a wrapper for a physical database connection. Each connection can be
30: * associated with an XID and can be in one of several states depending on which
31: * XAResource calls have been made against a given XID. This allows a StandardXAConnection
32: * to multiplex between different XIDs by selecting the appropriate stateful
33: * connection.
34: */
35: public class StandardXAStatefulConnection {
36:
37: public static int nextId; // used to allocate unique IDs
38: public int id; // unique ID for this stateful connection
39: public Connection con; // the phsyical database connection
40: private int state; // one of the states listed below
41: public StandardXADataSource dataSource; // used to log messages
42: Xid xid; // global TX associated with this connection (if any)
43: public boolean commitOnPrepare; // true if commit takes place on prepare
44: long timeout; // time when this transaction times out
45: boolean timedOut; // true if this transaction branch has timed out
46:
47: /**
48: * Creates a new stateful connection in the FREE state (NO_TRANSACTION)
49: */
50: public StandardXAStatefulConnection(
51: StandardXADataSource dataSource, Connection con) {
52: this .con = con;
53: this .dataSource = dataSource;
54: id = ++nextId; // allocate a unique ID for logging
55: this .state = Status.STATUS_NO_TRANSACTION;
56: dataSource.log.debug("StandardXAStatefulConnection created");
57: }
58:
59: /**
60: * Accessor methods for "state" property.
61: */
62: synchronized void setState(int newState) {
63: dataSource.log
64: .debug("StandardXAStatefulConnection:setState Stateful connection: "
65: + id + " (state before=" + state + ")");
66: state = newState;
67: dataSource.log
68: .debug("StandardXAStatefulConnection:setState Stateful connection: "
69: + id + " (state after=" + state + ")");
70: }
71:
72: int getState() {
73: return state;
74: }
75:
76: public String toString() {
77: StringBuffer sb = new StringBuffer();
78: sb.append("StandardXAStatefulConnection:\n");
79: sb.append(" commit on prepare =<" + this .commitOnPrepare
80: + ">\n");
81: sb.append(" timed out =<" + this .timedOut + ">\n");
82: sb.append(" id =<" + this .id + ">\n");
83: sb.append(" state =<" + this .state + ">\n");
84: sb.append(" time out =<" + this .timeout + ">\n");
85: sb.append(" xid =<" + this .xid + ">\n");
86:
87: return sb.toString();
88: }
89: }
|