All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.hajdbc.sql.ConnectionInvocationHandler Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
/*
 * HA-JDBC: High-Availability JDBC
 * Copyright (C) 2012  Paul Ferraro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package net.sf.hajdbc.sql;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.NClob;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import net.sf.hajdbc.Database;
import net.sf.hajdbc.durability.Durability;
import net.sf.hajdbc.invocation.InvocationStrategies;
import net.sf.hajdbc.invocation.InvocationStrategy;
import net.sf.hajdbc.invocation.Invoker;
import net.sf.hajdbc.util.StaticRegistry;
import net.sf.hajdbc.util.reflect.Methods;

/**
 * @author Paul Ferraro
 * @param  
 * @param 

*/ public class ConnectionInvocationHandler, P> extends ChildInvocationHandler> { private static final Set driverReadMethodSet = Methods.findMethods(Connection.class, "createStruct", "getAutoCommit", "getCatalog", "getClientInfo", "getHoldability", "getNetworkTimeout", "getSchema", "getTransactionIsolation", "getTypeMap", "getWarnings", "isClosed", "isCloseOnCompletion", "isReadOnly", "nativeSQL"); private static final Set databaseReadMethodSet = Methods.findMethods(Connection.class, "isValid"); private static final Set driverWriterMethodSet = Methods.findMethods(Connection.class, "abort", "clearWarnings", "closeOnCompletion", "setClientInfo", "setHoldability", "setNetworkTimeout", "setSchema", "setTypeMap"); private static final Set createStatementMethodSet = Methods.findMethods(Connection.class, "createStatement"); private static final Set prepareStatementMethodSet = Methods.findMethods(Connection.class, "prepareStatement"); private static final Set prepareCallMethodSet = Methods.findMethods(Connection.class, "prepareCall"); private static final Set setSavepointMethodSet = Methods.findMethods(Connection.class, "setSavepoint"); private static final Method setAutoCommitMethod = Methods.getMethod(Connection.class, "setAutoCommit", Boolean.TYPE); private static final Method commitMethod = Methods.getMethod(Connection.class, "commit"); private static final Method rollbackMethod = Methods.getMethod(Connection.class, "rollback"); private static final Method getMetaDataMethod = Methods.getMethod(Connection.class, "getMetaData"); private static final Method releaseSavepointMethod = Methods.getMethod(Connection.class, "releaseSavepoint", Savepoint.class); private static final Method rollbackSavepointMethod = Methods.getMethod(Connection.class, "rollback", Savepoint.class); private static final Method closeMethod = Methods.getMethod(Connection.class, "close"); private static final Method createArrayMethod = Methods.getMethod(Connection.class, "createArrayOf", String.class, Object[].class); private static final Method createBlobMethod = Methods.getMethod(Connection.class, "createBlob"); private static final Method createClobMethod = Methods.getMethod(Connection.class, "createClob"); private static final Method createNClobMethod = Methods.getMethod(Connection.class, "createNClob"); private static final Method createSQLXMLMethod = Methods.getMethod(Connection.class, "createSQLXML"); private static final Set endTransactionMethodSet = new HashSet(Arrays.asList(commitMethod, rollbackMethod, setAutoCommitMethod)); private static final Set createLocatorMethodSet = new HashSet(Arrays.asList(createBlobMethod, createClobMethod, createNClobMethod, createSQLXMLMethod)); private static final StaticRegistry phaseRegistry = new DurabilityPhaseRegistry(Arrays.asList(commitMethod, setAutoCommitMethod), Arrays.asList(rollbackMethod)); /** * Constructs a new ConnectionInvocationHandler * @param proxyFactory a factory for creating connection proxies */ public ConnectionInvocationHandler(ConnectionProxyFactory proxyFactory) { super(Connection.class, proxyFactory, null); } @Override protected ProxyFactoryFactory getProxyFactoryFactory(Connection connection, Method method, Object... parameters) throws SQLException { if (createStatementMethodSet.contains(method)) { return new StatementProxyFactoryFactory(this.getProxyFactory().getTransactionContext()); } if (prepareStatementMethodSet.contains(method)) { String sql = (String) parameters[0]; return new PreparedStatementProxyFactoryFactory(this.getProxyFactory().getTransactionContext(), this.getProxyFactory().extractLocks(sql), this.getProxyFactory().isSelectForUpdate(sql)); } if (prepareCallMethodSet.contains(method)) { String sql = (String) parameters[0]; return new CallableStatementProxyFactoryFactory(this.getProxyFactory().getTransactionContext(), this.getProxyFactory().extractLocks(sql)); } if (setSavepointMethodSet.contains(method)) { return new SavepointProxyFactoryFactory(); } if (method.equals(getMetaDataMethod)) { return new DatabaseMetaDataProxyFactoryFactory(); } if (method.equals(createArrayMethod)) { return new ArrayProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy()); } if (method.equals(createBlobMethod)) { return new BlobProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy()); } if (method.equals(createClobMethod)) { return new ClobProxyFactoryFactory(Clob.class, this.getProxyFactory().locatorsUpdateCopy()); } if (method.equals(createNClobMethod)) { return new ClobProxyFactoryFactory(NClob.class, this.getProxyFactory().locatorsUpdateCopy()); } if (method.equals(createSQLXMLMethod)) { return new SQLXMLProxyFactoryFactory(this.getProxyFactory().locatorsUpdateCopy()); } return super.getProxyFactoryFactory(connection, method, parameters); } /** * {@inheritDoc} */ @Override protected InvocationStrategy getInvocationStrategy(Connection connection, Method method, Object... parameters) throws SQLException { if (driverReadMethodSet.contains(method)) { return InvocationStrategies.INVOKE_ON_ANY; } if (databaseReadMethodSet.contains(method) || method.equals(getMetaDataMethod)) { return InvocationStrategies.INVOKE_ON_NEXT; } if (driverWriterMethodSet.contains(method) || method.equals(closeMethod) || createStatementMethodSet.contains(method)) { return InvocationStrategies.INVOKE_ON_EXISTING; } if (prepareStatementMethodSet.contains(method) || prepareCallMethodSet.contains(method) || createLocatorMethodSet.contains(method)) { return InvocationStrategies.INVOKE_ON_ALL; } if (endTransactionMethodSet.contains(method)) { return this.getProxyFactory().getTransactionContext().end(InvocationStrategies.END_TRANSACTION_INVOKE_ON_ALL, phaseRegistry.get(method)); } if (method.equals(rollbackSavepointMethod) || method.equals(releaseSavepointMethod)) { return InvocationStrategies.END_TRANSACTION_INVOKE_ON_ALL; } if (setSavepointMethodSet.contains(method)) { return InvocationStrategies.TRANSACTION_INVOKE_ON_ALL; } return super.getInvocationStrategy(connection, method, parameters); } /** * {@inheritDoc} */ @Override protected Invoker getInvoker(Connection connection, Method method, Object... parameters) throws SQLException { if (method.equals(releaseSavepointMethod) || method.equals(rollbackSavepointMethod)) { return this.getInvoker(Savepoint.class, 0, connection, method, parameters); } if (prepareStatementMethodSet.contains(method) || prepareCallMethodSet.contains(method)) { parameters[0] = this.getProxyFactory().evaluate((String) parameters[0]); } Invoker invoker = super.getInvoker(connection, method, parameters); if (endTransactionMethodSet.contains(method)) { return this.getProxyFactory().getTransactionContext().end(invoker, phaseRegistry.get(method)); } return invoker; } @Override protected void postInvoke(Invoker invoker, Connection proxy, Method method, Object... parameters) { if (driverWriterMethodSet.contains(method) || method.equals(setAutoCommitMethod)) { this.getProxyFactory().record(invoker); } else if (method.equals(closeMethod)) { this.getProxyFactory().getTransactionContext().close(); this.getProxyFactory().remove(); } else if (method.equals(releaseSavepointMethod)) { SavepointInvocationHandler handler = (SavepointInvocationHandler) Proxy.getInvocationHandler(parameters[0]); this.getProxyFactory().removeChild(handler.getProxyFactory()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy