net.sf.hajdbc.invocation.InvokeOnOneInvocationStrategy Maven / Gradle / Ivy
/*
* 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.invocation;
import java.util.SortedMap;
import java.util.TreeMap;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.ExceptionFactory;
import net.sf.hajdbc.Messages;
import net.sf.hajdbc.balancer.Balancer;
import net.sf.hajdbc.dialect.Dialect;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.sql.ProxyFactory;
import net.sf.hajdbc.state.StateManager;
/**
* @author paul
*
*/
public class InvokeOnOneInvocationStrategy implements InvocationStrategy
{
private static Logger logger = LoggerFactory.getLogger(InvokeOnOneInvocationStrategy.class);
public static interface DatabaseSelector
{
> D selectDatabase(Balancer balancer);
}
private final DatabaseSelector selector;
public InvokeOnOneInvocationStrategy(DatabaseSelector selector)
{
this.selector = selector;
}
/**
* {@inheritDoc}
*/
@Override
public , T, R, E extends Exception> SortedMap invoke(ProxyFactory factory, Invoker invoker) throws E
{
DatabaseCluster cluster = factory.getDatabaseCluster();
ExceptionFactory exceptionFactory = factory.getExceptionFactory();
Balancer balancer = cluster.getBalancer();
Dialect dialect = cluster.getDialect();
StateManager stateManager = cluster.getStateManager();
while (true)
{
D database = this.selector.selectDatabase(balancer);
if (database == null)
{
throw exceptionFactory.createException(Messages.NO_ACTIVE_DATABASES.getMessage(cluster));
}
T object = factory.get(database);
try
{
R result = balancer.invoke(invoker, database, object);
SortedMap resultMap = new TreeMap();
resultMap.put(database, result);
return resultMap;
}
catch (Exception e)
{
// If this database was concurrently deactivated, just ignore the failure
if (balancer.contains(database))
{
E exception = exceptionFactory.createException(e);
if (exceptionFactory.indicatesFailure(exception, dialect))
{
if (cluster.deactivate(database, stateManager))
{
logger.log(Level.ERROR, exception, Messages.DATABASE_DEACTIVATED.getMessage(), database, cluster);
}
}
else
{
throw exception;
}
}
}
}
}
}