KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > standard > StandardXAStatefulConnection


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * 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 JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26 import javax.transaction.Status JavaDoc;
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 JavaDoc 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 JavaDoc 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,
52         Connection JavaDoc con) {
53         this.con = con;
54         this.dataSource = dataSource;
55         id = ++nextId; // allocate a unique ID for logging
56
this.state = Status.STATUS_NO_TRANSACTION;
57         dataSource.log.debug("StandardXAStatefulConnection created");
58     }
59
60     /**
61      * Accessor methods for "state" property.
62      */

63     synchronized void setState(int newState) {
64         dataSource.log.debug(
65             "StandardXAStatefulConnection:setState Stateful connection: "
66                 + id
67                 + " (state before="
68                 + state
69                 + ")");
70         state = newState;
71         dataSource.log.debug(
72             "StandardXAStatefulConnection:setState Stateful connection: "
73                 + id
74                 + " (state after="
75                 + state
76                 + ")");
77     }
78
79     int getState() {
80         return state;
81     }
82
83     public String JavaDoc toString() {
84         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
85         sb.append("StandardXAStatefulConnection:\n");
86         sb.append(" commit on prepare =<"+this.commitOnPrepare+ ">\n");
87         sb.append(" timed out =<"+this.timedOut+ ">\n");
88         sb.append(" id =<"+this.id+ ">\n");
89         sb.append(" state =<"+this.state+ ">\n");
90         sb.append(" time out =<"+this.timeout+ ">\n");
91         sb.append(" xid =<"+this.xid+ ">\n");
92                 
93         return sb.toString();
94     }
95 }
96
Popular Tags