/*-----------------------------------------------------------------------------
 *
 * Enhydra JdbcS
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright 1997-2000 Lutris Technologies Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * www.enhydra.org/license/epl.html
 *
 * Software distributed under the License is distributed on an "ASIS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * -----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */
package org.enhydra.jdbc.standard;

import java.sql.Connection;
import javax.transaction.xa.Xid;
import javax.transaction.Status;

/**
 * Provides a wrapper for a physical database connection. Each connection can be
 * associated with an XID and can be in one of several states depending on which 
 * XAResource calls have been made against a given XID. This allows a StandardXAConnection
 * to multiplex between different XIDs by selecting the appropriate stateful
 * connection.
 */
public class StandardXAStatefulConnection  {
    
    public static int			nextId;		// used to allocate unique IDs
    public int				id;		// unique ID for this stateful connection
    public Connection			con;		// the phsyical database connection
    private int				state;		// one of the states listed below
    public StandardXADataSource		dataSource;	// used to log messages
    Xid					xid;          // global TX associated with this connection (if any)
    public boolean			commitOnPrepare;// true if commit takes place on prepare
    long				timeout;	// time when this transaction times out
    boolean				timedOut;	// true if this transaction branch has timed out
    
    /**
     * Creates a new stateful connection in the FREE state (NO_TRANSACTION)
     */
    public StandardXAStatefulConnection (StandardXADataSource dataSource, Connection con) {
	this.con = con;
	this.dataSource = dataSource;	
	id = ++nextId;					// allocate a unique ID for logging
	this.state = Status.STATUS_NO_TRANSACTION;
	dataSource.log("StandardXAStatefulConnection created");
    } // method StandardXAStatefulConnection
    
    /**
     * Accessor methods for "state" property.
     */
    synchronized void setState (int newState) {
	dataSource.log("StandardXAStatefulConnection:setState Stateful connection: "+id+" (state before="+state+")");
	state = newState;
	dataSource.log("StandardXAStatefulConnection:setState Stateful connection: "+id+" (state after="+state+")");
    } // method setState
    
    int getState () {
	return state;
    } // method getState
    
} // class StandardXAStatefulConnection 
