net.sf.hajdbc.state.sql.SQLStateManager 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.state.sql;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.DatabaseProperties;
import net.sf.hajdbc.ExceptionType;
import net.sf.hajdbc.IdentifiableMatcher;
import net.sf.hajdbc.cache.lazy.LazyDatabaseProperties;
import net.sf.hajdbc.cache.simple.SimpleDatabaseMetaDataProvider;
import net.sf.hajdbc.dialect.Dialect;
import net.sf.hajdbc.dialect.DialectFactory;
import net.sf.hajdbc.durability.Durability;
import net.sf.hajdbc.durability.DurabilityListener;
import net.sf.hajdbc.durability.InvocationEvent;
import net.sf.hajdbc.durability.InvocationEventImpl;
import net.sf.hajdbc.durability.InvokerEvent;
import net.sf.hajdbc.durability.InvokerEventImpl;
import net.sf.hajdbc.durability.InvokerResult;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.pool.Pool;
import net.sf.hajdbc.pool.PoolFactory;
import net.sf.hajdbc.pool.sql.ConnectionFactory;
import net.sf.hajdbc.pool.sql.ConnectionPoolProvider;
import net.sf.hajdbc.sql.DriverDatabase;
import net.sf.hajdbc.state.DatabaseEvent;
import net.sf.hajdbc.state.DurabilityListenerAdapter;
import net.sf.hajdbc.state.SerializedDurabilityListener;
import net.sf.hajdbc.state.StateManager;
import net.sf.hajdbc.tx.TransactionIdentifierFactory;
import net.sf.hajdbc.util.Objects;
import net.sf.hajdbc.util.Resources;
import net.sf.hajdbc.util.ServiceLoaders;
/**
* @author Paul Ferraro
*/
public class SQLStateManager> implements StateManager, ConnectionFactory, SerializedDurabilityListener
{
private static final String STATE_TABLE = "cluster_state";
private static final String DATABASE_COLUMN = "database_id";
private static final String INVOCATION_TABLE = "cluster_invocation";
private static final String INVOKER_TABLE = "cluster_invoker";
private static final String TRANSACTION_COLUMN = "tx_id";
private static final String PHASE_COLUMN = "phase_id";
private static final String EXCEPTION_COLUMN = "exception_id";
private static final String RESULT_COLUMN = "result";
static final String SELECT_STATE_SQL = MessageFormat.format("SELECT {1} FROM {0}", STATE_TABLE, DATABASE_COLUMN);
static final String INSERT_STATE_SQL = MessageFormat.format("INSERT INTO {0} ({1}) VALUES (?)", STATE_TABLE, DATABASE_COLUMN);
static final String DELETE_STATE_SQL = MessageFormat.format("DELETE FROM {0} WHERE {1} = ?", STATE_TABLE, DATABASE_COLUMN);
static final String TRUNCATE_STATE_SQL = MessageFormat.format("DELETE FROM {0}", STATE_TABLE);
static final String SELECT_INVOCATION_SQL = MessageFormat.format("SELECT {1}, {2}, {3} FROM {0}", INVOCATION_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN, EXCEPTION_COLUMN);
static final String INSERT_INVOCATION_SQL = MessageFormat.format("INSERT INTO {0} ({1}, {2}, {3}) VALUES (?, ?, ?)", INVOCATION_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN, EXCEPTION_COLUMN);
static final String DELETE_INVOCATION_SQL = MessageFormat.format("DELETE FROM {0} WHERE {1} = ? AND {2} = ?", INVOCATION_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN);
static final String SELECT_INVOKER_SQL = MessageFormat.format("SELECT {1}, {2}, {3}, {4} FROM {0}", INVOKER_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN, DATABASE_COLUMN, RESULT_COLUMN);
static final String INSERT_INVOKER_SQL = MessageFormat.format("INSERT INTO {0} ({1}, {2}, {3}) VALUES (?, ?, ?)", INVOKER_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN, DATABASE_COLUMN);
static final String UPDATE_INVOKER_SQL = MessageFormat.format("UPDATE {0} SET {4} = ? WHERE {1} = ? AND {2} = ? AND {3} = ?", INVOKER_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN, DATABASE_COLUMN, RESULT_COLUMN);
static final String DELETE_INVOKER_SQL = MessageFormat.format("DELETE FROM {0} WHERE {1} = ? AND {2} = ?", INVOKER_TABLE, TRANSACTION_COLUMN, PHASE_COLUMN);
private static final String CREATE_INVOCATION_SQL = MessageFormat.format("CREATE TABLE {0} ({1} {2} NOT NULL, {3} {4} NOT NULL, {5} {6} NOT NULL, PRIMARY KEY ({1}, {3}))", INVOCATION_TABLE, TRANSACTION_COLUMN, "{0}", PHASE_COLUMN, "{1}", EXCEPTION_COLUMN, "{2}");
private static final String CREATE_INVOKER_SQL = MessageFormat.format("CREATE TABLE {0} ({1} {2} NOT NULL, {3} {4} NOT NULL, {5} {6} NOT NULL, {7} {8}, PRIMARY KEY ({1}, {3}, {5}))", INVOKER_TABLE, TRANSACTION_COLUMN, "{0}", PHASE_COLUMN, "{1}", DATABASE_COLUMN, "{2}", RESULT_COLUMN, "{3}");
private static final String CREATE_STATE_SQL = MessageFormat.format("CREATE TABLE {0} ({1} {2} NOT NULL, PRIMARY KEY ({1}))", STATE_TABLE, DATABASE_COLUMN, "{0}");
private static Logger logger = LoggerFactory.getLogger(SQLStateManager.class);
private final DurabilityListener listener;
private final DatabaseCluster cluster;
private final PoolFactory poolFactory;
private final DriverDatabase database;
private String password;
private Driver driver;
private Pool pool;
public SQLStateManager(DatabaseCluster cluster, DriverDatabase database, PoolFactory poolFactory)
{
this.cluster = cluster;
this.database = database;
this.poolFactory = poolFactory;
this.listener = new DurabilityListenerAdapter(this, cluster.getTransactionIdentifierFactory());
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.StateManager#getActiveDatabases()
*/
@Override
public Set getActiveDatabases()
{
Query> query = new Query>()
{
@Override
public Set execute(Connection connection) throws SQLException
{
Set set = new TreeSet();
PreparedStatement statement = connection.prepareStatement(SELECT_STATE_SQL);
try
{
ResultSet resultSet = statement.executeQuery();
while (resultSet.next())
{
set.add(resultSet.getString(1));
}
return set;
}
finally
{
Resources.close(statement);
}
}
};
try
{
return this.execute(query);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
return Collections.emptySet();
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.StateManager#setActiveDatabases(java.util.Set)
*/
@Override
public void setActiveDatabases(final Set databases)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
Statement s = connection.createStatement();
try
{
s.executeUpdate(TRUNCATE_STATE_SQL);
}
finally
{
Resources.close(s);
}
PreparedStatement statement = connection.prepareStatement(INSERT_STATE_SQL);
try
{
for (String database: databases)
{
statement.clearParameters();
statement.setString(1, database);
statement.addBatch();
}
statement.executeBatch();
}
finally
{
Resources.close(statement);
}
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.DatabaseClusterListener#activated(net.sf.hajdbc.state.DatabaseEvent)
*/
@Override
public void activated(final DatabaseEvent event)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
SQLStateManager.this.execute(connection, INSERT_STATE_SQL, event);
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.DatabaseClusterListener#deactivated(net.sf.hajdbc.state.DatabaseEvent)
*/
@Override
public void deactivated(final DatabaseEvent event)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
SQLStateManager.this.execute(connection, DELETE_STATE_SQL, event);
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
void execute(Connection connection, String sql, DatabaseEvent event) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(sql);
try
{
statement.setString(1, event.getSource());
statement.executeUpdate();
}
finally
{
Resources.close(statement);
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.SerializedDurabilityListener#beforeInvocation(byte[], byte, byte)
*/
@Override
public void beforeInvocation(final byte[] transactionId, final byte phase, final byte exceptionType)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(INSERT_INVOCATION_SQL);
try
{
statement.setBytes(1, transactionId);
statement.setByte(2, phase);
statement.setByte(3, exceptionType);
statement.executeUpdate();
}
finally
{
Resources.close(statement);
}
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.SerializedDurabilityListener#afterInvocation(byte[], byte)
*/
@Override
public void afterInvocation(final byte[] transactionId, final byte phase)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
SQLStateManager.this.execute(connection, DELETE_INVOKER_SQL, transactionId, phase);
SQLStateManager.this.execute(connection, DELETE_INVOCATION_SQL, transactionId, phase);
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.SerializedDurabilityListener#beforeInvoker(byte[], byte, java.lang.String)
*/
@Override
public void beforeInvoker(final byte[] transactionId, final byte phase, final String databaseId)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(INSERT_INVOKER_SQL);
try
{
statement.setBytes(1, transactionId);
statement.setByte(2, phase);
statement.setString(3, databaseId);
statement.executeUpdate();
}
finally
{
Resources.close(statement);
}
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.SerializedDurabilityListener#afterInvoker(byte[], byte, java.lang.String, byte[])
*/
@Override
public void afterInvoker(final byte[] transactionId, final byte phase, final String databaseId, final byte[] result)
{
Transaction transaction = new Transaction()
{
@Override
public void execute(Connection connection) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(UPDATE_INVOKER_SQL);
try
{
statement.setBytes(1, result);
statement.setBytes(2, transactionId);
statement.setByte(3, phase);
statement.setString(4, databaseId);
statement.executeUpdate();
}
finally
{
Resources.close(statement);
}
}
};
try
{
this.execute(transaction);
}
catch (SQLException e)
{
logger.log(Level.ERROR, e, e.getMessage());
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.durability.DurabilityListener#beforeInvocation(net.sf.hajdbc.durability.InvocationEvent)
*/
@Override
public void beforeInvocation(InvocationEvent event)
{
this.listener.beforeInvocation(event);
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.durability.DurabilityListener#afterInvocation(net.sf.hajdbc.durability.InvocationEvent)
*/
@Override
public void afterInvocation(InvocationEvent event)
{
this.listener.afterInvocation(event);
}
void execute(Connection connection, String sql, byte[] transactionId, byte phase) throws SQLException
{
PreparedStatement statement = connection.prepareStatement(sql);
try
{
statement.setBytes(1, transactionId);
statement.setByte(2, phase);
statement.executeUpdate();
}
finally
{
Resources.close(statement);
}
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.state.StateManager#recover()
*/
@Override
public Map> recover()
{
final TransactionIdentifierFactory> txIdFactory = this.cluster.getTransactionIdentifierFactory();
Query
© 2015 - 2025 Weber Informatics LLC | Privacy Policy