/*
* Use of this J2EE Connectors Sample Source Code file is governed by
* the following modified BSD license:
*
* Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduct the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
* SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
* OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
* SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
* OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
* LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
/* */
package com.sun.connector.cciblackbox;
import javax.resource.cci.*;
import java.sql.*;
import java.util.Map;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.IllegalStateException;
import javax.resource.spi.*;
import javax.resource.NotSupportedException;
/**
* This implementation class represents an application level connection
*handle that is used by a component to access an EIS instance.
*
* @author Sheetal Vartak
*/
public class CciConnection implements javax.resource.cci.Connection {
private boolean destroyed;
private CciManagedConnection mc;
// if mc is null, means connection is invalid
CciConnection(CciManagedConnection mc) {
this.mc = mc;
}
CciManagedConnection getManagedConnection() {
return mc;
}
public Interaction createInteraction() throws ResourceException {
return new CciInteraction(this);
}
public javax.resource.cci.LocalTransaction getLocalTransaction()
throws ResourceException {
try {
java.sql.Connection con = getJdbcConnection();
if (con.getTransactionIsolation() == con.TRANSACTION_NONE) {
throw new ResourceException("Local Transaction not supported!!");
}
} catch(Exception e) {
throw new ResourceException(e.getMessage());
}
return new CciLocalTransactionImpl(mc);
}
public void setAutoCommit(boolean autoCommit) throws ResourceException {
try {
java.sql.Connection con = getJdbcConnection();
if (con.getTransactionIsolation() == con.TRANSACTION_NONE) {
throw new ResourceException("Local Transaction not " +
"supported!!");
}
con.setAutoCommit(autoCommit);
}catch(Exception e) {
throw new ResourceException(e.getMessage());
}
}
public boolean getAutoCommit() throws ResourceException {
boolean val = false;
try{
java.sql.Connection con = getJdbcConnection();
if (con.getTransactionIsolation() == con.TRANSACTION_NONE) {
throw new ResourceException("Local Transaction not " +
"supported!!");
}
val = con.getAutoCommit();
}catch(SQLException e) {
throw new ResourceException(e.getMessage());
}
return val;
}
public ResultSetInfo getResultSetInfo() throws ResourceException {
throw new NotSupportedException("ResultSet is not supported.");
}
public void close() throws ResourceException {
if (mc == null) return; // already be closed
mc.removeCciConnection(this);
mc.sendEvent(ConnectionEvent.CONNECTION_CLOSED, null, this);
mc = null;
}
public ConnectionMetaData getMetaData() throws ResourceException {
return new CciConnectionMetaDataImpl(mc);
}
void associateConnection(CciManagedConnection newMc)
throws ResourceException {
try {
checkIfValid();
} catch (ResourceException ex) {
throw new IllegalStateException("Connection is invalid");
}
// dissociate handle with current managed connection
mc.removeCciConnection(this);
// associate handle with new managed connection
newMc.addCciConnection(this);
mc = newMc;
}
void checkIfValid() throws ResourceException {
if (mc == null) {
throw new ResourceException("Connection is invalid");
}
}
java.sql.Connection getJdbcConnection() throws SQLException {
java.sql.Connection con=null;
try {
checkIfValid();
// mc.getJdbcConnection() returns a SQL connection object
con = mc.getJdbcConnection();
} catch (ResourceException ex) {
throw new SQLException("Connection is invalid.");
}
return con;
}
void invalidate() {
mc = null;
}
private void checkIfDestroyed() throws ResourceException {
if (destroyed) {
throw new IllegalStateException("Managed connection is closed");
}
}
}