package art.servers.gost.access.types;
|
|
import art.library.utils.licence.Licence;
|
import art.library.utils.synchro.Mutex;
|
import art.servers.Shared;
|
import art.servers.gost.access.configuration.ConfigurationDetail_Database;
|
import java.sql.Connection;
|
import java.sql.DriverManager;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
public class DatabasePoolConnection
|
{
|
private String name = null;
|
private ConfigurationDetail_Database configuration = null;
|
|
private boolean initialised = false;
|
private List<Connection> free = new ArrayList<Connection>();
|
private List<Connection> used = new ArrayList<Connection>();
|
private Mutex mutexList = new Mutex();
|
private Mutex mutexConnection = new Mutex();
|
|
|
public DatabasePoolConnection(String name, ConfigurationDetail_Database configuration)
|
{
|
this.name = name;
|
this.configuration = configuration;
|
this.initialise();
|
}
|
|
|
|
public Connection getConnection(boolean autocommit) throws Exception
|
{
|
mutexConnection.lockWrite(); // Locks until available free connection
|
mutexList.lockWrite();
|
initialise(); // May be first time there was no connection to database
|
|
try
|
{
|
Connection connection = free.get(0);
|
free.remove(connection);
|
|
if (connection.isClosed() == true)
|
{
|
connection = openConnection();
|
}
|
|
used.add(connection);
|
connection.setAutoCommit(autocommit);
|
return connection;
|
}
|
finally
|
{
|
if (free.size() > 0)
|
{
|
// We can release only if there are free connections
|
|
mutexConnection.releaseWrite();
|
}
|
|
mutexList.releaseWrite();
|
}
|
}
|
|
|
|
|
|
public boolean executeUpdate (Connection connection, String sql)
|
{
|
PreparedStatement statement = null;
|
|
try
|
{
|
statement = connection.prepareStatement(sql);
|
statement.executeUpdate();
|
return true;
|
}
|
catch (Exception e)
|
{
|
}
|
|
close(statement);
|
return false;
|
}
|
|
|
|
|
public void vacuum(String tablename, Connection connection) throws Exception
|
{
|
executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_enabled = true, toast.autovacuum_enabled = true)");
|
executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_vacuum_scale_factor = 0.0)");
|
executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_vacuum_threshold = 1000)");
|
executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_analyze_scale_factor = 0.0)");
|
executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_analyze_threshold = 1000)");
|
}
|
|
|
|
|
public void commit(Connection connection) throws Exception
|
{
|
try
|
{
|
connection.commit();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
public void rollback(Connection connection) throws Exception
|
{
|
try
|
{
|
connection.rollback();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
|
public void close(PreparedStatement statement)
|
{
|
try
|
{
|
statement.close();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
public void close(ResultSet resultset)
|
{
|
try
|
{
|
resultset.close();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
public void close(PreparedStatement statement, ResultSet resultset)
|
{
|
close(resultset);
|
close(statement);
|
}
|
|
|
|
public void releaseConnection(Connection connection, PreparedStatement statement)
|
{
|
close(statement);
|
releaseConnection(connection);
|
}
|
|
|
|
public void releaseConnection(Connection connection, PreparedStatement statement, ResultSet resultset)
|
{
|
close(resultset);
|
close(statement);
|
releaseConnection(connection);
|
}
|
|
|
public void commitConnection(Connection connection, PreparedStatement statement, ResultSet resultset) throws Exception
|
{
|
close(resultset);
|
close(statement);
|
connection.commit();
|
releaseConnection(connection);
|
}
|
|
|
public void rollbackConnection(Connection connection, PreparedStatement statement, ResultSet resultset) throws Exception
|
{
|
close(resultset);
|
close(statement);
|
connection.rollback();
|
releaseConnection(connection);
|
}
|
|
|
public void releaseConnection(Connection connection)
|
{
|
if (connection == null) return;
|
|
mutexList.lockWrite();
|
|
try
|
{
|
used.remove(connection);
|
free.add(connection);
|
}
|
finally
|
{
|
if (free.size() == 1)
|
{
|
// Means there were no available connections
|
// We can release mutex
|
|
mutexConnection.releaseWrite();
|
}
|
|
mutexList.releaseWrite();
|
}
|
}
|
|
|
|
public void close()
|
{
|
mutexList.lockWrite();
|
|
try
|
{
|
for (int i=0; i<free.size(); i++)
|
{
|
try
|
{
|
free.get(i).close();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
|
for (int i=0; i<used.size(); i++)
|
{
|
try
|
{
|
used.get(i).close();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
free.clear();
|
used.clear();
|
}
|
finally
|
{
|
mutexList.releaseWrite();
|
}
|
}
|
|
|
|
|
// <editor-fold defaultstate="collapsed" desc="Private">
|
|
|
|
private void initialise()
|
{
|
try
|
{
|
if (initialised == false)
|
{
|
for (int i=0; i<configuration.pool; i++)
|
{
|
free.add(openConnection());
|
}
|
}
|
|
initialised = true;
|
}
|
catch (Exception exception)
|
{
|
Shared.traceError(name, Shared.getMessage("Database pool initialization"), exception);
|
|
for (int i=0; i<free.size(); i++)
|
{
|
try
|
{
|
free.get(i).close();
|
}
|
catch (Exception e)
|
{
|
}
|
}
|
|
free.clear();
|
}
|
}
|
|
private Connection openConnection(ConfigurationDetail_Database configuration, boolean autocommit) throws Exception
|
{
|
String user = Licence.decrypt(configuration.user);
|
String password = Licence.decrypt(configuration.password);
|
Connection connection = (Connection) DriverManager.getConnection(configuration.connectionString, user, password);
|
connection.setAutoCommit(autocommit);
|
return connection;
|
}
|
|
|
|
private Connection openConnection() throws Exception
|
{
|
String user = Licence.decrypt(configuration.user);
|
String password = Licence.decrypt(configuration.password);
|
Connection connection = (Connection) DriverManager.getConnection(configuration.connectionString, user, password);
|
connection.setAutoCommit(true);
|
return connection;
|
}
|
|
|
// </editor-fold>
|
|
|
|
}
|