Commit 5e0a72e2 authored by R.W.Majeed's avatar R.W.Majeed

fixed duplicate and to early close of database connection

parent 6693f118
......@@ -4,8 +4,8 @@ import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;
import de.sekmi.histream.AbnormalFlag;
......@@ -244,11 +244,20 @@ public class I2b2Extractor implements ObservationSupplier {
}
@Override
public void close() throws SQLException {
Statement st = rs.getStatement();
rs.close();
st.close();
dbc.close();
public void close() {
log.info("Closing extractor "+this.toString());
// Statement st = rs.getStatement();
try{
rs.close();
}catch( SQLException e){
log.log(Level.WARNING,"Failed to close recortset",e);
}
// st.close();
try{
dbc.close();
}catch( SQLException e){
log.log(Level.WARNING,"Failed to close connection",e);
}
}
public void dump() throws SQLException{
......
......@@ -97,11 +97,16 @@ public class I2b2ExtractorFactory implements AutoCloseable, ObservationExtractor
}
private void createTemporaryConceptTable(Connection dbc, Iterable<String> concepts) throws SQLException{
// delete table if previously existing
try( Statement s = dbc.createStatement() ){
s.executeUpdate("DROP TABLE IF EXISTS temp_concepts");
}
try( Statement s = dbc.createStatement() ){
s.executeUpdate("CREATE TEMPORARY TABLE temp_concepts(concept VARCHAR(255) PRIMARY KEY)");
}
try( PreparedStatement ps
= dbc.prepareStatement("INSERT INTO temp_concepts(concept) VALUES(?)") ){
// TODO do we need to make sure that there are no duplicate concepts???
for( String concept : concepts ){
ps.clearParameters();
ps.clearWarnings();
......@@ -139,8 +144,10 @@ public class I2b2ExtractorFactory implements AutoCloseable, ObservationExtractor
// TODO move connection and prepared statement to I2b2Extractor
PreparedStatement ps = null;
ResultSet rs = null;
try( Connection dbc = ds.getConnection() ){
dbc.setAutoCommit(false);
Connection dbc = null;
try{ // no try with resource, because we need to pass the connection to the extractor
dbc = ds.getConnection();
dbc.setAutoCommit(true);
StringBuilder b = new StringBuilder(600);
b.append("SELECT ");
b.append(SELECT_PARAMETERS+" FROM "+SELECT_TABLE+" ");
......@@ -171,14 +178,13 @@ public class I2b2ExtractorFactory implements AutoCloseable, ObservationExtractor
b.append("WHERE f.start_date BETWEEN ? AND ? ");
b.append(SELECT_ORDER_GROUP);
log.info("SQL: "+b.toString());
ps = prepareStatement(dbc, b.toString());
ps.setTimestamp(1, start_min);
ps.setTimestamp(2, start_max);
rs = ps.executeQuery();
return new I2b2Extractor(this, dbc, rs);
}catch( SQLException e ){
// XXX maybe we don't need to do this, since the connection is closed anyway
// clean up
if( rs != null ){
rs.close();
......@@ -186,6 +192,9 @@ public class I2b2ExtractorFactory implements AutoCloseable, ObservationExtractor
if( ps != null ){
ps.close();
}
if( dbc != null ){
dbc.close();
}
throw e;
}
}
......
Markdown is supported
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