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

net.sf.hajdbc.sql.LocalTransactionContext 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.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 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 invoke(ProxyFactory proxy, Invoker invoker) throws E
			{
				InvocationStrategy durabilityStrategy = LocalTransactionContext.this.durability.getInvocationStrategy(strategy, phase, LocalTransactionContext.this.transactionId);
				
				try
				{
					return durabilityStrategy.invoke(proxy, invoker);
				}
				finally
				{
					LocalTransactionContext.this.unlock();
				}
			}
		};
	}

	/**
	 * {@inheritDoc}
	 * @see net.sf.hajdbc.sql.TransactionContext#end(net.sf.hajdbc.invocation.Invoker, net.sf.hajdbc.durability.Durability.Phase)
	 */
	@Override
	public  Invoker end(final Invoker invoker, Durability.Phase phase)
	{
		if (this.transactionId == null) return invoker;

		return this.durability.getInvoker(invoker, phase, this.transactionId, ExceptionType.SQL.getExceptionFactory());
	}

	/**
	 * @see net.sf.hajdbc.sql.TransactionContext#close()
	 */
	@Override
	public void close()
	{
		// Tsk, tsk... User neglected to commit/rollback transaction
		if (this.transactionId != null)
		{
			this.unlock();
		}
	}

	void lock()
	{
		this.lock.lock();
		this.transactionId = this.transactionIdFactory.createTransactionIdentifier();
	}
	
	void unlock()
	{
		this.lock.unlock();
		this.transactionId = null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy