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

net.sf.hajdbc.invocation.InvokeOnAnyInvocationStrategy 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.invocation;

import java.util.Map;
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 Ferraro
 */
public class InvokeOnAnyInvocationStrategy implements InvocationStrategy
{
	private static Logger logger = LoggerFactory.getLogger(ExistingResultsCollector.class);

	private final InvocationStrategy strategy;
	
	public InvokeOnAnyInvocationStrategy(InvocationStrategy strategy)
	{
		this.strategy = strategy;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public , T, R, E extends Exception> SortedMap invoke(ProxyFactory factory, Invoker invoker) throws E
	{
		DatabaseCluster cluster = factory.getDatabaseCluster();
		Balancer balancer = cluster.getBalancer();
		Dialect dialect = cluster.getDialect();
		StateManager stateManager = cluster.getStateManager();

		for (Map.Entry entry: factory.entries())
		{
			D database = entry.getKey();
			
			// If this database is no longer active, just skip it.
			if (balancer.contains(database))
			{
				try
				{
					R result = invoker.invoke(database, entry.getValue());
					
					SortedMap resultMap = new TreeMap();
					resultMap.put(database, result);
					return resultMap;
				}
				catch (Exception e)
				{
					// If this database was concurrently deactivated, just ignore the failure
					if (cluster.getBalancer().contains(database))
					{
						ExceptionFactory exceptionFactory = factory.getExceptionFactory();
						E exception = exceptionFactory.createException(e);
						
						if (exceptionFactory.indicatesFailure(exception, dialect) && (cluster.getBalancer().size() > 1))
						{
							if (cluster.deactivate(database, stateManager))
							{
								logger.log(Level.ERROR, exception, Messages.DATABASE_DEACTIVATED.getMessage(), database, cluster);
							}
						}
						else
						{
							throw exception;
						}
					}
				}
			}
		}
		
		// If no existing databases could handle the request, delegate to another invocation strategy
		return this.strategy.invoke(factory, invoker);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy