Unverified Commit 5f8e0225 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(statement): close Select statement (#11)

Instead of closing the ResultSet, close the parent statement. Might
reduce resource leak issues.
parent 3a5fd2ba
......@@ -40,6 +40,8 @@ public class Database {
private Context ctx;
private Statement selectStatement;
public Database(final String driver, final String url, final String username, final String password)
throws ClassNotFoundException, SQLException {
Class.forName(driver);
......@@ -53,6 +55,10 @@ public class Database {
}
public void disconnect() throws SQLException, NamingException {
if(selectStatement != null) {
selectStatement.close();
selectStatement = null;
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
......@@ -61,15 +67,18 @@ public class Database {
}
}
public ResultSet select(final String query) throws SQLException {
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
public ResultSet select(final String query) throws ConnectorException, SQLException {
if(selectStatement != null) {
throw new ConnectorException("A Statement is already opened.");
}
selectStatement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return statement.executeQuery(query);
return selectStatement.executeQuery(query);
}
public boolean executeCommand(final String command) throws SQLException, ConnectorException {
Statement statement = null;
boolean isExecuted = false;
boolean isExecuted = false;
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
isExecuted = statement.execute(command);
......@@ -83,6 +92,13 @@ public class Database {
return isExecuted;
}
/**
* Does not produce an output result. A Statement is created, executed and closed.
* @param commands, the list of SQL command to execute
* @param commit , commit after the batch execution
* @throws SQLException
* @throws ConnectorException
*/
public void executeBatch(final List<String> commands, final boolean commit) throws SQLException, ConnectorException {
Statement statement = null;
try {
......
......@@ -14,7 +14,6 @@
*/
package org.bonitasoft.connectors.database.datasource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -48,7 +47,6 @@ public class DatasourceConnector implements Connector {
private String separator;
private Properties properties;
private Database database;
private ResultSet resultSet;
@Override
......@@ -114,14 +112,6 @@ public class DatasourceConnector implements Connector {
@Override
public void disconnect() throws ConnectorException {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {
throw new ConnectorException(e);
}
}
if (database != null) {
try {
database.disconnect();
......@@ -136,8 +126,7 @@ public class DatasourceConnector implements Connector {
final String command = script.toUpperCase().trim();
final Map<String, Object> result = new HashMap<>(2);
if (command.startsWith("SELECT")) {
resultSet = database.select(script);
result.put("resultset", resultSet);
result.put("resultset", database.select(script));
} else {
database.executeCommand(script);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment