com.github.asteraether.tomlib.sqlib.CachedStatementConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TomLib Show documentation
Show all versions of TomLib Show documentation
A simple collection of usefull things in java.
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);
}
}
}