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

com.jk.data.datasource.JKSession Maven / Gradle / Ivy

/*
 * Copyright 2002-2022 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://smart-api.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jk.data.datasource;

import java.sql.Connection;
import java.sql.SQLException;

import com.jk.core.exceptions.JKDataAccessException;

// TODO: Auto-generated Javadoc
/**
 * The Class JKSession.
 */
public class JKSession {

	/** The connection. */
	private Connection connection;

	/** The commit. */
	private boolean commit;

	/** The roll back only. */
	private boolean rollBackOnly;

	/** The closed. */
	boolean closed;

	/** The parent session. */
	private JKSession parentSession;

	/** The connection manager. */
	private JKDataSource connectionManager;

	/**
	 * Instantiates a new JK session.
	 *
	 * @param connection the connection
	 * @throws JKDataAccessException the JK data access exception
	 */
	private JKSession(final Connection connection) throws JKDataAccessException {
		this.connection = connection;
		try {
			connection.setAutoCommit(false);
		} catch (final SQLException e) {
			throw new JKDataAccessException(e);
		}
	}

	/**
	 * Instantiates a new JK session.
	 *
	 * @param connectionManager the connection manager
	 * @throws JKDataAccessException the JK data access exception
	 */
	public JKSession(final JKDataSource connectionManager) throws JKDataAccessException {
		this(connectionManager.getConnection());
		this.connectionManager = connectionManager;
	}

	/**
	 * Instantiates a new JK session.
	 *
	 * @param parentSession the parent session
	 */
	public JKSession(final JKSession parentSession) {
		this.parentSession = parentSession;
	}

	/**
	 * Close.
	 *
	 * @throws JKDataAccessException the JK data access exception
	 */
	public void close() throws JKDataAccessException {
		if (this.parentSession != null) {
			// Just return , because this would be called from method that
			// doesn't know it has been
			// called from outer session, so we just wait until the parent
			// session close it self
			return;
		}
		try {
			if (this.connection == null || this.connection.isClosed()) {
				throw new IllegalStateException("Invalid call to Session.close on closed session");
			}
			if (isCommit() && !isRollbackOnly()) {
				this.connection.commit();
			} else {
				this.connection.rollback();
			}
		} catch (final SQLException e) {
			throw new JKDataAccessException(e);
		} finally {
			// the abstract resource manage will check internally on the
			// nullable and isClosed properties for the connection
			// GeneralUtility.printStackTrace();
			// System.err.println("Closing connection from session");
			this.connectionManager.close(this.connection);
			setClosed(true);
		}
	}

	/**
	 * Close.
	 *
	 * @param commit the commit
	 * @throws JKDataAccessException the JK data access exception
	 */
	public void close(final boolean commit) throws JKDataAccessException {
		commit(commit);
		close();
	}

	/**
	 * Commit.
	 *
	 * @param commit the commit
	 */
	protected void commit(final boolean commit) {
		if (this.parentSession != null) {
			this.parentSession.commit(commit);
		} else {
			if (!commit) {
				this.rollBackOnly = true;
			} else {
				this.commit = true;
			}
		}
	}

	/**
	 * Gets the connection.
	 *
	 * @return the connection
	 */
	public Connection getConnection() {
		return this.parentSession != null ? this.parentSession.getConnection() : this.connection;
	}

	/**
	 * Gets the connection manager.
	 *
	 * @return the connection manager
	 */
	public JKDataSource getConnectionManager() {
		final JKDataSource manager = this.parentSession != null ? this.parentSession.getConnectionManager() : this.connectionManager;
		if (manager == null) {
			throw new IllegalStateException("Connectino manager connot be null");
		}
		return manager;
	}

	/**
	 * Gets the parent session.
	 *
	 * @return the parent session
	 */
	public JKSession getParentSession() {
		return this.parentSession;
	}

	/**
	 * Checks if is closed.
	 *
	 * @return true, if is closed
	 */
	public boolean isClosed() {
		return this.parentSession != null ? this.parentSession.isClosed() : this.closed;
	}

	/**
	 * Checks if is commit.
	 *
	 * @return true, if is commit
	 */
	public boolean isCommit() {
		return this.parentSession != null ? this.parentSession.isCommit() : this.commit;
	}

	/**
	 * Checks if is rollback only.
	 *
	 * @return true, if is rollback only
	 */
	private boolean isRollbackOnly() {
		return this.rollBackOnly;
	}

	/**
	 * Sets the closed.
	 *
	 * @param closed the new closed
	 */
	private void setClosed(final boolean closed) {
		this.closed = closed;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy