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

fr.ms.log4jdbc.context.ConnectionContextDefault Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.context;

import java.sql.Connection;

import fr.ms.lang.delegate.DefaultStringMakerFactory;
import fr.ms.lang.delegate.DefaultSyncLongFactory;
import fr.ms.lang.delegate.StringMakerFactory;
import fr.ms.lang.delegate.SyncLongFactory;
import fr.ms.lang.reflect.ReflectionUtils;
import fr.ms.lang.stringmaker.impl.StringMaker;
import fr.ms.lang.sync.impl.SyncLong;
import fr.ms.log4jdbc.rdbms.GenericRdbmsSpecifics;
import fr.ms.log4jdbc.rdbms.RdbmsSpecifics;
import fr.ms.log4jdbc.utils.ServicesJDBC;
import fr.ms.util.logging.Logger;
import fr.ms.util.logging.LoggerManager;

/**
 *
 * @see Marco4J
 *
 *
 * @author Marco Semiao
 *
 */
public class ConnectionContextDefault {

	private final static Logger LOG = LoggerManager.getLogger(TransactionContextDefault.class);

	private final static SyncLongFactory syncLongFactory = DefaultSyncLongFactory.getInstance();

	private final static SyncLong totalConnectionNumber = syncLongFactory.newLong();

	private final static SyncLong openConnection = syncLongFactory.newLong();

	private Connection connection;

	protected long connectionNumber;

	protected String driverName;

	protected String url;

	protected final RdbmsSpecifics rdbmsSpecifics;

	protected String transactionIsolation = "UNKNOWN";

	{
		this.connectionNumber = totalConnectionNumber.incrementAndGet();
		long openNumber = openConnection.incrementAndGet();

		if (LOG.isDebugEnabled()) {
			LOG.debug("Nouvelle Connection - connectionNumber : " + connectionNumber + " - openConnection : "
					+ openNumber);
		}
	}

	public ConnectionContextDefault(final Connection connection, final Class clazz, final String url) {
		this.connection = connection;
		this.driverName = clazz.getName();
		this.rdbmsSpecifics = getRdbms(driverName);
		this.url = url;
	}

	public void close() {
		long oNumber = openConnection.decrementAndGet();
		if (LOG.isDebugEnabled()) {
			LOG.debug("Fermeture Connnection - transactionNumber : " + oNumber);
		}
	}

	public Connection getConnection() {
		return connection;
	}

	public long getConnectionNumber() {
		return connectionNumber;
	}

	public SyncLong getTotalConnectionNumber() {
		return totalConnectionNumber;
	}

	public SyncLong getOpenConnection() {
		return openConnection;
	}

	public String getDriverName() {
		return driverName;
	}

	public String getUrl() {
		return url;
	}

	public RdbmsSpecifics getRdbmsSpecifics() {
		return rdbmsSpecifics;
	}

	public String getTransactionIsolation() {
		return transactionIsolation;
	}

	public void setTransactionIsolation(int transactionIsolation) {
		String constantName = ReflectionUtils.constantName(Connection.class, transactionIsolation);
		this.transactionIsolation = constantName;
	}

	public String toString() {
		final StringMakerFactory stringFactory = DefaultStringMakerFactory.getInstance();
		final StringMaker buffer = stringFactory.newString();

		buffer.append("ConnectionContextDefault [driverName=");
		buffer.append(driverName);
		buffer.append(", url=");
		buffer.append(url);
		buffer.append(", connectionNumber=");
		buffer.append(connectionNumber);
		buffer.append(", rdbmsSpecifics=");
		buffer.append(rdbmsSpecifics);
		buffer.append(", transactionIsolation=");
		buffer.append(transactionIsolation);
		buffer.append("]");

		return buffer.toString();
	}

	private final static RdbmsSpecifics getRdbms(final String driverClass) {
		RdbmsSpecifics rdbmsSpecifics = ServicesJDBC.getRdbmsSpecifics(driverClass);
		if (rdbmsSpecifics == null) {
			rdbmsSpecifics = GenericRdbmsSpecifics.getInstance();
		}

		return rdbmsSpecifics;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy