/*
* Copyright 2013 Simon Thiel
*
* This file is part of SitJar.
*
* SitJar 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.
*
* SitJar 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 SitJar. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
*/
package sit.db;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author simon
*/
public class Connection {
private final java.sql.Connection con;
private final ArrayList<Statement> statements = new ArrayList();
public Connection(java.sql.Connection con) {
this.con = con;
}
protected synchronized void closeAndClearStatements() throws SQLException{
for (Statement stmt: statements){
stmt.close();
}
statements.clear();
}
public synchronized PreparedStatement createPrepStmt(String sql, int autoGeneratedKeys) throws SQLException {
PreparedStatement result = con.prepareStatement(sql, autoGeneratedKeys);
statements.add(result);
return result;
}
public synchronized PreparedStatement createPrepStmt(String sql) throws SQLException{
PreparedStatement result = con.prepareStatement(sql);
statements.add(result);
return result;
}
public synchronized Statement createStatement() throws SQLException {
Statement result = con.createStatement();
statements.add(result);
return result;
}
public DatabaseMetaData getMetaData() throws SQLException {
return con.getMetaData();
}
void shutdown() {
try{
closeAndClearStatements();
} catch (SQLException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}