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