FileDocCategorySizeDatePackage
MsqlConnection.javaAPI DocExample10297Tue Jan 01 00:00:00 GMT 1980COM.imaginary.sql.msql

MsqlConnection

public class MsqlConnection extends Object implements Connection
The MsqlConnection class is an implementation of the JDBC Connection interface that represents a database transaction. This class should never be directly instantiated by an application, but instead should be gotten by calling DriverManager.getConnection().
Last modified 97/04/19
version
@(#) MsqlConnection.java 1.3@(#)
author
George Reese (borg@imaginary.com)
see
java.sql.DriverManager#getConnection

Fields Summary
private String
catalog
private boolean
connecting
private Socket
connection
private Statement
current
private MsqlInputStream
input
private int
isolation
private MsqlOutputStream
output
private String
user
private int
version
private SQLWarning
warnings
Constructors Summary
MsqlConnection(String host, int port, String db, Properties p)


    // Since this should only be instantiated by MsqlDriver, this is
    // package visibility
           
      
	super();
	user = (String)p.get("user");
	if( user == null || user.equals("") ) {
	    user = "nobody";
	}
	connect(host, port, user);
	selectDatabase(db);
	synchronized( this ) {
	    connecting = false;
	    notify();
	}
    
Methods Summary
private voidawaitConnection()

	synchronized( this ) {
	    while( connecting ) {
		try {
		    wait();
		}
		catch( InterruptedException e ) {
		    if( connecting ) {
			throw new MsqlException(e);
		    }
		}
	    }
	}
    
private voidcleanConnection()

	synchronized( this ) {
	    if( current != null ) {
		try {
		    current.close();
		}
		catch( SQLException e ) {
		    e.printStackTrace();
		}
		current = null;
	    }
	    connecting = false;
	    connection = null;
	}
	input = null;
	output = null;
	user = null;
    
public synchronized voidclearWarnings()
Sets the SQLWarning chain to null.

exception
java.sql.SQLException never thrown

	warnings = null;
    
public synchronized voidclose()
Closes this database connection. It will also close any associated Statement instance.

exception
java.sql.SQLException thrown if problems occur

	Exception except = null;

	awaitConnection();
	if( isClosed() ) {
	    throw new SQLException("This Connection is already closed.");
	}
	// Any current statement has ownership of the streams
	// We therefore have to close *before* sending data
	if( current != null ) {
	    current.close();
	    current = null;
	}
	// Let mSQL know we are closing down and close the output stream
	try {
	    output.writeAsciiString("1");
	    output.flush();
	    output.close();
	    output = null;
	}
	catch( IOException e ) {
	    output = null;
	    except = e;
	}
	// close the input stream
	try {
	    input.close();
	    input = null;
	}
	catch( IOException e ) {
	    input = null;
	    except = e;
	}
	// close the connection
	try {
	    connection.close();
	}
	catch( IOException e ) {
	    connection = null;
	    throw new MsqlException(e);
	}
	if( except != null ) {
	    throw new MsqlException(except);
	}
    
public voidcommit()
This is a NO-OP for mSQL. All statements are always auto-committed.

exception
java.sql.SQLException never thrown

    
private voidconnect(java.lang.String host, int port, java.lang.String user)

	try {
	    String tmp;
	    
	    connection = new Socket(host, port);
	    input = new MsqlInputStream(connection.getInputStream());
	    output = new MsqlOutputStream(connection.getOutputStream());
	    tmp = input.readAsciiString();
	    // Check for mSQL version
	    if( tmp.startsWith("0:22:") ) {
		version = 2; // version 2.0
	    }
	    else if( tmp.startsWith("0:6:") ) {
		version = 1; // version 1.0.x
	    }
	    else {
		throw new SQLException("Unsupported mSQL version.");
	    }
	    // Check if user was validated
	    output.writeAsciiString(user);
	    tmp = input.readAsciiString();
	    if( !tmp.startsWith("-100:") ) {
		throw new SQLException("Access to server denied.");
	    }
	}
	catch( IOException e ) {
	    cleanConnection();
	    throw new SQLException("Connection failed.");
	}
    
public java.sql.StatementcreateStatement()
This JDBC method creates an instance of MsqlStatement and returns it. If there are currently any open MsqlStatement's, it will close them. Not that mSQL does not provide a way to interrupt executing statements, so it has to wait for any pending statements to finish before closing them.

return
a new MsqlStatement instance
exception
java.sql.SQLException an error occurred in creating the Statement instance, likely raised by the constructor

	awaitConnection();
	synchronized( this ) {
	    if( current != null ) {
		try {
		    current.close();
		}
		catch( SQLException e ) {
		    e.printStackTrace();
		}
	    }
	    current = new MsqlStatement(this, input, output);
	    return current;
	}
    
public booleangetAutoCommit()
This method always returns true since mSQL is always in auto-commit mode.

return
true--always
exception
java.sql.SQLException never thrown

	return true;
    
public synchronized java.lang.StringgetCatalog()
Provides the catalog name. mSQL does not support catalogs, however.

exception
java.sql.SQLException never thrown

	return catalog;
    
public java.sql.DatabaseMetaDatagetMetaData()
This method is not yet implemented.

	return null;
    
public synchronized intgetTransactionIsolation()

return
the transaction isolation, not supported by mSQL
exception
java.sql.SQLException never thrown

	return isolation;
    
public java.lang.StringgetUser()

return
the user name used for this Connection
exception
java.sql.SQLException thrown if the connection is never made

	awaitConnection();
	return user;
    
public synchronized java.sql.SQLWarninggetWarnings()

return
the SQLWarning chain for this Connection
exception
java.sql.SQLException never thrown

	return warnings;
    
public synchronized booleanisClosed()

return
true if this Connection has been closed
exception
java.sql.SQLException never thrown

	awaitConnection();
	return (connection == null);
    
public booleanisReadOnly()

return
false--always

	return false;
    
public java.lang.StringnativeSQL(java.lang.String sql)
This gives the driver an opportunity to turn JDBC compliant SQL into mSQL specific SQL. My feeling is why bother.

exception
java.sql.SQLException never thrown

	return sql;
    
public java.sql.CallableStatementprepareCall(java.lang.String sql)
Callable statements are not supported by mSQL. This will therefore always throw an exception.

	throw new SQLException("mSQL does not support stored procedures.");
    
public java.sql.PreparedStatementprepareStatement(java.lang.String sql)
Prepared statements are not supported by mSQL. This will therefore always throw an exception.

	throw new SQLException("mSQL does not support prepared statements.");
    
public voidrollback()
This method always errors since you cannot rollback an mSQL transaction.

	throw new SQLException("mSQL exception: mSQL does not support " +
			       "rollbacks.");
    
private voidselectDatabase(java.lang.String database)

	String tmp;

	try {
	    output.writeAsciiString("2 " + database);
	    tmp = input.readAsciiString();
	    if( tmp.startsWith("-1:") ) {
		throw new MsqlException(tmp);
	    }
	}
	catch( IOException e ) {
	    cleanConnection();
	    throw new MsqlException(e);
	}
    
public voidsetAutoCommit(boolean b)
This method will thrown an exception if you try to turn auto-commit off since JDBC does not support transactional logic.

param
b should always be true
exception
java.sql.SQLException thrown if the param is false

	if( b ) {
	    return;
	}
	throw new SQLException("mSQL must always be auto-commit = true.");
    
public synchronized voidsetCatalog(java.lang.String str)
Sets the catalog name for this connection. mSQL does not support catalogs, so this method is a NO-OP.

exception
java.sql.SQLException never thrown

	catalog = str;
    
public voidsetReadOnly(boolean b)
mSQL does not support read-only mode.

exception
java.sql.SQLException alway thrown

	throw new SQLException("mSQL does not support read-only mode.");
    
public synchronized voidsetTransactionIsolation(int x)
This is not supported by mSQL, thus this is a NO-OP.

exception
java.sql.SQLException never thrown

	isolation = x;