FileDocCategorySizeDatePackage
ConnectionFactoryImpl.javaAPI DocGlassfish v2 API19507Fri May 04 22:35:06 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.impl

ConnectionFactoryImpl

public class ConnectionFactoryImpl extends Object implements ConnectionFactory, Serializable
author
Craig Russell
version
0.1

Fields Summary
private String
URL
private String
userName
private String
password
private String
driverName
private int
maxPool
private int
minPool
private int
msInterval
private int
loginTimeout
private int
msWait
private int
_txIsolation
private transient PrintWriter
logWriter
private transient boolean
_configured
private transient com.sun.jdo.spi.persistence.support.sqlstore.connection.ConnectionManager
connectionManager
private static final transient ResourceBundle
messages
I18N message handler
Constructors Summary
public ConnectionFactoryImpl()
Creates new default ConnectionFactoryImpl object


	

          
   
  
	//connectionManager = new ConnectionManager();
  
public ConnectionFactoryImpl(String URL, String userName, String password, String driverName)
Creates new ConnectionFactoryImpl object with user info

param
URL connection URL
param
userName database user
param
password database user password
param
driverName driver name

		this.driverName = driverName;
		this.URL = URL;
		this.userName = userName;
		this.password = password;

  
public ConnectionFactoryImpl(String URL, String userName, String password, String driverName, int minPool, int maxPool)
Creates new ConnectionFactoryImpl object with user and connection info

param
URL connection URL
param
userName database user
param
password database user password
param
driverName driver name
param
minPool minimum number of connections
param
maxPool maximum number of connections

		this.driverName = driverName;
		this.URL = URL;
		this.userName = userName;
		this.password = password;
		this.minPool = minPool;
		this.maxPool = maxPool;

  
Methods Summary
private voidassertConnectionWait()
INTERNAL Asserts that MsWait and MsInterval are properly configured

                if ( msWait < 0 )  
                {
                        throw new JDOUserException(I18NHelper.getMessage(messages,
                                             "connection.connectionmanager.mswaitvalue")); // NOI18N
                }
                else if ( msInterval < 0 || msInterval > msWait || (msWait > 0 && msInterval == 0) )
                {
                                throw new JDOUserException(I18NHelper.getMessage(messages,
                                             "connection.connectionmanager.msintervalvalue")); // NOI18N
                }
        
private voidassertNotConfigured()
INTERNAL Asserts that change to the property is allowed

                if ( _configured) {
                        throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
                                "persistencemanagerfactoryimpl.configured")); //NOI18N
                }
        
private voidcloseConnection(java.sql.Connection conn)
Close a connection

                try
                {
			if (conn != null) conn.close();
                }
                catch (Exception e)
                {
                        // Recover?
                }
 
                conn = null;
 
        
public voidconfigured(boolean flag)
INTERNAL Marks Connectionfactory as fully configured

param
flag boolean flag

		_configured = flag;
	
public booleanequals(java.lang.Object obj)
Determines whether obj is a ConnectionFactoryImpl with the same configuration

param
obj The possibly null object to check.
return
true if obj is equal to this ConnectionFactoryImpl; false otherwise.

	if ((obj != null) && (obj instanceof ConnectionFactoryImpl) ) {
		ConnectionFactoryImpl cf = (ConnectionFactoryImpl)obj;
		return (cf.URL.equals(this.URL) && cf.userName.equals(this.userName) && cf.driverName.equals(this.driverName) && cf.password.equals(this.password));
	}
	return false;
  
public java.sql.ConnectiongetConnection()
Returns java.sql.Connection

return
connection as java.sql.Connection

	// Delegate to ConnectionManager
	try {
		if (connectionManager == null)
			initialize();

                Connection conn = connectionManager.getConnection();
		conn.setTransactionIsolation(_txIsolation);

		return conn;

	} catch (SQLException e) {
		String sqlState = e.getSQLState();
		int  errorCode = e.getErrorCode();

		if (sqlState == null)
		{
			throw new JDODataStoreException(I18NHelper.getMessage(messages,
                                "connectionefactoryimpl.sqlexception", "null", "" + errorCode), e); //NOI18N
		}
		else
		{
			throw new JDODataStoreException(I18NHelper.getMessage(messages,
                                "connectionefactoryimpl.sqlexception", sqlState, "" + errorCode), e); //NOI18N
		}
	} catch (Exception e) {
                throw new JDOCanRetryException(I18NHelper.getMessage(messages, 
                                "connectionefactoryimpl.getconnection"), e); //NOI18N
	}
  
public java.lang.StringgetDriverName()
Returns JDBC driver name

return
driver name

	return driverName;
	//return connectionManager.getDriverName();
  
public java.io.PrintWritergetLogWriter()
Returns the LogWriter to which messages should be sent

return
logWriter

	return logWriter;
  
public intgetLoginTimeout()
Returns the number of seconds to wait for a new connection to be established to the data source

return
wait time in seconds

	if (connectionManager == null)
                return loginTimeout;

	try {
                return connectionManager.getLoginTimeout();
	} catch (Exception e) {
                return 0;
	}
  
public intgetMaxPool()
Returns maximum number of connections in the connection pool

return
connection maxPool

	return maxPool;
	// return connectionManager.getMaxPool();
  
public intgetMinPool()
Returns minimum number of connections in the connection pool

return
connection minPool

	return minPool;
	//return connectionManager.getMinPool();
  
public intgetMsInterval()
Returns the amount of time, in milliseconds, between the connection manager's attempts to get a pooled connection.

return
the length of the interval between tries in milliseconds

	if (connectionManager == null)
		return msInterval;

	return connectionManager.getMsInterval();
  
public intgetMsWait()
Returns the number of milliseconds to wait for an available connection from the connection pool before throwing an exception

return
number in milliseconds

	if (connectionManager == null)
                return msWait;

	return connectionManager.getMsWait();
  
public intgetTransactionIsolation()
Gets this ConnectionFactory's current transaction isolation level.

return
the current transaction isolation mode value as java.sql.Connection.TRANSACTION_*

	if (connectionManager == null)
                return _txIsolation;

	Connection conn = null;
	try {
		// Delegate to the Connection
                if (_txIsolation == -1)
		{
		    synchronized(this)
		    {
			//Double check that it was not set before
                	if (_txIsolation == -1)
			{
				conn = connectionManager.getConnection();
				_txIsolation = conn.getTransactionIsolation();
			}
		    }
		}

		return _txIsolation;
	} catch (Exception e) {
                throw new JDOFatalException(null, e);
	}
	finally
	{
		closeConnection(conn);
	}
  
public java.lang.StringgetURL()
Returns connection URL

return
connection URL

	return URL;
	//return connectionManager.getURL();
  
public java.lang.StringgetUserName()
Returns database user name

return
current database user name

	return userName;
	//return connectionManager.getUserName();
  
public inthashCode()
Computes the hash code of this ConnectionFactoryImpl.

return
A hash code of the owning ConnectionFactoryImpl as an int.

  	return URL.hashCode() + userName.hashCode() + password.hashCode() + driverName.hashCode();
  
private synchronized voidinitialize()
INTERNAL Attempts to create new connectionManager Throws JDOFatalException

		// If connectionManager was already initialized by another thread
		if (connectionManager != null)
			return;

		try {
		// Verify msWait/msInterval values
			assertConnectionWait();

                // need to use this constructor, as it calls internaly startUp() method
                // to initialize extra variables and enable pooling
			//java.sql.DriverManager.setLogWriter(logWriter);
               		connectionManager = new ConnectionManager(
                        		driverName,
                        		URL,
                        		userName,
                        		password,
                        		minPool,
                        		maxPool
                        );
			// MsWait MUST be set BEFORE MsInterval
                        connectionManager.setMsWait(this.msWait);
			connectionManager.setMsInterval(this.msInterval);
                        connectionManager.setLoginTimeout(this.loginTimeout);

                        if (_txIsolation > 0)
                                setTransactionIsolation(_txIsolation);
                        else
                                _txIsolation = getTransactionIsolation();

			// finished all configuration
			this.configured(true);

        	} catch (JDOException e) {
                	throw e;
        	} catch (Exception e) {
                	throw new JDOFatalException(null, e);
		}
	
public voidsetDriverName(java.lang.String driverName)
Sets JDBC driver name

param
driverName JDBC driver name

	// REMOVE WHEN SUPPORTED:
	//unsupported();

	assertNotConfigured();
	// Delegate to ConnectionManager: this.driverName = driverName;
	if(connectionManager == null)
	{
		this.driverName = driverName;
	}
	else
	{
		try {
			connectionManager.setDriverName(driverName);
		} catch (Exception e) {
			throw new JDOFatalException(null, e);
		}
	}
  
public voidsetLogWriter(java.io.PrintWriter logWriter)
Sets the LogWriter to which messages should be sent

param
pw logWriter

	assertNotConfigured();
	this.logWriter = logWriter; //RESOLVE
  
public voidsetLoginTimeout(int loginTimeout)
Sets the number of seconds to wait for a new connection to be established to the data source

param
loginTimeout wait time in seconds

	assertNotConfigured();
	if(connectionManager == null) { 
                // Nothing to do yet 
                this.loginTimeout = loginTimeout; 
                return; 
        } 

	// Delegate to ConnectionManager: this.loginTimeout = loginTimeout;
	try {
                connectionManager.setLoginTimeout(loginTimeout);
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
  
public voidsetMaxPool(int maxPool)
Sets maximum number of connections in the connection pool

param
maxPool maximum number of connections

	assertNotConfigured();
	if(connectionManager == null) { 
                // Nothing to do yet 
                this.maxPool = maxPool; 
                return; 
        } 

	// Delegate to ConnectionManager: this.maxPool = maxPool;
	try {
                connectionManager.setMaxPool(maxPool);
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
  
public voidsetMinPool(int minPool)
Sets minimum number of connections in the connection pool

param
minPool minimum number of connections

	assertNotConfigured();
	if(connectionManager == null) { 
                // Nothing to do yet 
                this.minPool = minPool; 
                return; 
        } 

	// Delegate to ConnectionManager: this.minPool = minPool;
	try {
                connectionManager.setMinPool(minPool);
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
  
public voidsetMsInterval(int msInterval)
Sets the amount of time, in milliseconds, between the connection manager's attempts to get a pooled connection.

param
msInterval the interval between attempts to get a database connection, in milliseconds.

	assertNotConfigured();
	if(connectionManager == null) { 
                // Nothing to do yet 
                this.msInterval = msInterval; 
                return; 
        } 

	// Delegate to ConnectionManager: this.msInterval = msInterval;
	try {
                connectionManager.setMsInterval(msInterval);
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
  
public voidsetMsWait(int msWait)
Sets the number of milliseconds to wait for an available connection from the connection pool before throwing an exception

param
msWait number in milliseconds

	assertNotConfigured();
	if(connectionManager == null) { 
                // Nothing to do yet 
                this.msWait = msWait; 
                return; 
        } 

	// Delegate to ConnectionManager: this.msWait = msWait;
	try {
                connectionManager.setMsWait(msWait);
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
  
public voidsetPassword(java.lang.String password)
Sets database user password

param
password database user password

	// REMOVE WHEN SUPPORTED:
	//unsupported();

	assertNotConfigured();
	// Delegate to ConnectionManager: this.password = password;
	if(connectionManager == null)
        {
                this.password = password;
        }
        else
        {
		try {
                	connectionManager.setPassword(password);
		} catch (Exception e) {
			throw new JDOFatalException(null, e);
		}
	}
  
public voidsetTransactionIsolation(int level)
Sets transaction isolation level for all connections of this ConnectionFactory. All validation is done by java.sql.Connection itself, so e.g. while Oracle will not allow to set solation level to TRANSACTION_REPEATABLE_READ, this method does not have any explicit restrictions

param
level - one of the java.sql.Connection.TRANSACTION_* isolation values

	assertNotConfigured();
	if(connectionManager == null) {
		// Nothing to do yet
		_txIsolation = level;
		return;
	}

	// verify that database supports it
	Connection conn = null;
	try {
		conn = connectionManager.getConnection();
		DatabaseMetaData dbMetaData = conn.getMetaData();
		if(dbMetaData.supportsTransactionIsolationLevel(level)) 
		{
                	_txIsolation = level;
		} 
		else 
		{
			throw new JDOFatalException(I18NHelper.getMessage(messages,
                                "connectionefactoryimpl.isolationlevel_notsupported", //NOI18N
				level)); 
		}
	} catch (Exception e) {
		throw new JDOFatalException(null, e);
	}
	finally
	{
		closeConnection(conn);
	}
  
public voidsetURL(java.lang.String URL)
Sets JDBC connection URL

param
URL connection URL

	// REMOVE WHEN SUPPORTED:
	//unsupported();

	assertNotConfigured();
	// Delegate to ConnectionManager: this.URL = URL;
	if(connectionManager == null) 
        {
                this.URL = URL;
        } 
        else 
        { 
                try {
                	connectionManager.setURL(URL);
		} catch (Exception e) {
			throw new JDOFatalException(null, e);
		}
	}
  
public voidsetUserName(java.lang.String userName)
Sets database user

param
userName database user

	// REMOVE WHEN SUPPORTED:
	//unsupported();

	assertNotConfigured();
	// Delegate to ConnectionManager: this.userName = userName;
	if(connectionManager == null)
        {
                this.userName = userName;
	}
	else
        {
                try {
                	connectionManager.setUserName(userName);
		} catch (Exception e) {
			throw new JDOFatalException(null, e);
		}
	}
  
private voidunsupported()
INTERNAL Throws JDOUnsupportedOptionException

		throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
			"persistencemanagerfactoryimpl.notsupported")); //NOI18N