001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (C) 2012 Paul Ferraro 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package net.sf.hajdbc.sql; 019 020import java.lang.reflect.Method; 021import java.sql.SQLException; 022import java.util.Set; 023 024import javax.sql.DataSource; 025 026import net.sf.hajdbc.invocation.InvocationStrategies; 027import net.sf.hajdbc.invocation.InvocationStrategy; 028import net.sf.hajdbc.util.reflect.Methods; 029 030/** 031 * @author Paul Ferraro 032 * 033 */ 034public class DataSourceInvocationHandler extends CommonDataSourceInvocationHandler<DataSource, DataSourceDatabase, DataSourceProxyFactory> 035{ 036 private static final Set<Method> getConnectionMethods = Methods.findMethods(DataSource.class, "getConnection"); 037 038 public DataSourceInvocationHandler(DataSourceProxyFactory factory) 039 { 040 super(DataSource.class, factory); 041 } 042 043 /** 044 * {@inheritDoc} 045 * @see net.sf.hajdbc.sql.CommonDataSourceInvocationHandler#getInvocationStrategy(javax.sql.CommonDataSource, java.lang.reflect.Method, java.lang.Object[]) 046 */ 047 @Override 048 protected InvocationStrategy getInvocationStrategy(DataSource dataSource, Method method, Object... parameters) throws SQLException 049 { 050 if (getConnectionMethods.contains(method)) 051 { 052 return InvocationStrategies.TRANSACTION_INVOKE_ON_ALL; 053 } 054 return super.getInvocationStrategy(dataSource, method, parameters); 055 } 056 057 @Override 058 protected ProxyFactoryFactory<DataSource, DataSourceDatabase, DataSource, SQLException, ?, ? extends Exception> getProxyFactoryFactory(DataSource object, Method method, Object... parameters) throws SQLException 059 { 060 if (getConnectionMethods.contains(method)) 061 { 062 TransactionContext<DataSource, DataSourceDatabase> context = new LocalTransactionContext<DataSource, DataSourceDatabase>(this.getProxyFactory().getDatabaseCluster()); 063 064 return new ConnectionProxyFactoryFactory<DataSource, DataSourceDatabase, DataSource>(context); 065 } 066 067 return super.getProxyFactoryFactory(object, method, parameters); 068 } 069}