FileDocCategorySizeDatePackage
BorrowedConnectionProxy.javaAPI DocHibernate 3.2.53635Mon Nov 06 06:27:28 GMT 2006org.hibernate.jdbc

BorrowedConnectionProxy

public class BorrowedConnectionProxy extends Object implements InvocationHandler
A proxy for borrowed connections which funnels all requests back into the ConnectionManager from which it was borrowed to be properly handled (in terms of connection release modes).

Note: the term borrowed here refers to connection references obtained via {@link org.hibernate.Session#connection()} for application usage.

author
Steve Ebersole

Fields Summary
private static final Class[]
PROXY_INTERFACES
private final ConnectionManager
connectionManager
private boolean
useable
Constructors Summary
public BorrowedConnectionProxy(ConnectionManager connectionManager)


	   
		this.connectionManager = connectionManager;
	
Methods Summary
public static java.sql.ConnectiongenerateProxy(ConnectionManager connectionManager)
Generates a Connection proxy wrapping the connection managed by the passed connection manager.

param
connectionManager The connection manager to wrap with the connection proxy.
return
The generated proxy.

		BorrowedConnectionProxy handler = new BorrowedConnectionProxy( connectionManager );
		return ( Connection ) Proxy.newProxyInstance(
				getProxyClassLoader(),
		        PROXY_INTERFACES,
		        handler
		);
	
public static java.lang.ClassLoadergetProxyClassLoader()
Determines the appropriate class loader to which the generated proxy should be scoped.

return
The class loader appropriate for proxy construction.

		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		if ( cl == null ) {
			cl = BorrowedConnectionProxy.class.getClassLoader();
		}
		return cl;
	
public static java.sql.ConnectiongetWrappedConnection(java.sql.Connection connection)
Convience method for unwrapping a connection proxy and getting a handle to an underlying connection.

param
connection The connection (proxy) to be unwrapped.
return
The unwrapped connection.

		if ( connection != null && connection instanceof ConnectionWrapper ) {
			return ( ( ConnectionWrapper ) connection ).getWrappedConnection();
		}
		else {
			return connection;
		}
	
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
{@inheritDoc}

		if ( "close".equals( method.getName() ) ) {
			connectionManager.releaseBorrowedConnection();
			return null;
		}
		// should probably no-op commit/rollback here, at least in JTA scenarios
		if ( !useable ) {
			throw new HibernateException( "connnection proxy not usable after transaction completion" );
		}

		if ( "getWrappedConnection".equals( method.getName() ) ) {
			return connectionManager.getConnection();
		}

		try {
			return method.invoke( connectionManager.getConnection(), args );
		}
		catch( InvocationTargetException e ) {
			throw e.getTargetException();
		}
	
public static voidrenderUnuseable(java.sql.Connection connection)
Marks a borrowed connection as no longer usable.

param
connection The connection (proxy) to be marked.

		if ( connection != null && Proxy.isProxyClass( connection.getClass() ) ) {
			InvocationHandler handler = Proxy.getInvocationHandler( connection );
			if ( BorrowedConnectionProxy.class.isAssignableFrom( handler.getClass() ) ) {
				( ( BorrowedConnectionProxy ) handler ).useable = false;
			}
		}