Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.sql.Connection;
import java.sql.SQLException;
import java.util.SortedMap;
import java.util.concurrent.locks.Lock;
import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.ExceptionType;
import net.sf.hajdbc.durability.Durability;
import net.sf.hajdbc.invocation.InvocationStrategy;
import net.sf.hajdbc.invocation.Invoker;
import net.sf.hajdbc.tx.TransactionIdentifierFactory;
/**
* @author Paul Ferraro
* @param
* @param
*/
public class LocalTransactionContext> implements TransactionContext
{
final Durability durability;
private final Lock lock;
private final TransactionIdentifierFactory extends Object> transactionIdFactory;
volatile Object transactionId;
/**
* @param cluster
*/
public LocalTransactionContext(DatabaseCluster cluster)
{
this.lock = cluster.getLockManager().readLock(null);
this.durability = cluster.getDurability();
this.transactionIdFactory = cluster.getTransactionIdentifierFactory();
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.sql.TransactionContext#start(net.sf.hajdbc.invocation.InvocationStrategy, java.sql.Connection)
*/
@Override
public InvocationStrategy start(final InvocationStrategy strategy, final Connection connection) throws SQLException
{
if (this.transactionId != null) return strategy;
if (connection.getAutoCommit())
{
return new InvocationStrategy()
{
@Override
public , T, R, E extends Exception> SortedMap
invoke(ProxyFactory proxy, Invoker invoker) throws E
{
LocalTransactionContext.this.lock();
try
{
InvocationStrategy durabilityStrategy = LocalTransactionContext.this.durability.getInvocationStrategy(strategy, Durability.Phase.COMMIT, LocalTransactionContext.this.transactionId);
return durabilityStrategy.invoke(proxy, invoker);
}
finally
{
LocalTransactionContext.this.unlock();
}
}
};
}
return new InvocationStrategy()
{
@Override
public , T, R, E extends Exception> SortedMap
invoke(ProxyFactory proxy, Invoker invoker) throws E
{
LocalTransactionContext.this.lock();
try
{
return strategy.invoke(proxy, invoker);
}
catch (Throwable e)
{
throw proxy.getExceptionFactory().createException(e);
}
finally
{
LocalTransactionContext.this.unlock();
}
}
};
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.sql.TransactionContext#start(net.sf.hajdbc.invocation.Invoker, java.sql.Connection)
*/
@Override
public Invoker start(final Invoker invoker, Connection connection) throws SQLException
{
if ((this.transactionId == null) || !connection.getAutoCommit()) return invoker;
return new Invoker()
{
@Override
public R invoke(D database, T object) throws SQLException
{
return LocalTransactionContext.this.durability.getInvoker(invoker, Durability.Phase.COMMIT, LocalTransactionContext.this.transactionId, ExceptionType.SQL.getExceptionFactory()).invoke(database, object);
}
};
}
/**
* {@inheritDoc}
* @see net.sf.hajdbc.sql.TransactionContext#end(net.sf.hajdbc.invocation.InvocationStrategy, net.sf.hajdbc.durability.Durability.Phase)
*/
@Override
public InvocationStrategy end(final InvocationStrategy strategy, final Durability.Phase phase)
{
if (this.transactionId == null) return strategy;
return new InvocationStrategy()
{
@Override
public , T, R, E extends Exception> SortedMap