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

com.github.asteraether.tomlib.sqlib.CachedStatementConnection Maven / Gradle / Ivy

The newest version!
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.asteraether.tomlib.sqlib;

import com.github.asteraether.tomlib.sqlib.exceptions.SQLNoConnectionException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;

/**
 *
 * @author Thomas
 */
public class CachedStatementConnection {

    private final LinkedList queue = new LinkedList<>();
    private final Connection conn;

    /**
     *
     * @param conn
     */
    public CachedStatementConnection(Connection conn) {
        this.conn = conn;
    }

    /**
     *
     * @return @throws SQLNoConnectionException
     * @throws SQLException
     */
    public Statement getStatement() throws SQLNoConnectionException, SQLException {
        if (conn == null || conn.isClosed()) {
            throw new SQLNoConnectionException("No connection present");
        }

        return queue.isEmpty() ? conn.createStatement() : queue.poll();
    }

    /**
     *
     * @param stat
     * @throws SQLException
     */
    public void releaseStatement(Statement stat) throws SQLException {
        if (!stat.isClosed()) {
            queue.offer(stat);
        }
    }

    /**
     *
     * @throws SQLException
     */
    public void closeStatements() throws SQLException {
        for (Statement statement : queue) {
            statement.close();
            queue.remove(statement);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy