1 16 package scriptella.driver.hsqldb; 17 18 import scriptella.jdbc.JdbcConnection; 19 import scriptella.jdbc.JdbcUtils; 20 import scriptella.spi.ConnectionParameters; 21 22 import java.sql.Connection ; 23 import java.sql.Statement ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 34 public class HsqlConnection extends JdbcConnection { 35 40 public static final String SHUTDOWN_ON_EXIT = "shutdown_on_exit"; 41 42 private static final Logger LOG = Logger.getLogger(HsqlConnection.class.getName()); 43 private boolean shutdownOnExit; 44 45 50 HsqlConnection(Connection con, ConnectionParameters parameters) { 51 super(con, parameters); 52 } 53 54 @Override 55 protected void init(ConnectionParameters parameters) { 56 super.init(parameters); 57 shutdownOnExit = parameters.getBooleanProperty(SHUTDOWN_ON_EXIT, true) 58 && isInprocess(parameters.getUrl()); 59 } 60 61 void shutdown() { 62 assert shutdownOnExit; 63 Connection con = getNativeConnection(); 64 assert con != null; try { 66 if (con.isClosed()) { 67 LOG.info("Unable to correctly shutdown in-process HSQLDB. Connection has already already been closed"); 68 return; 69 } 70 Statement st = con.createStatement(); 71 st.execute("SHUTDOWN"); 72 JdbcUtils.closeSilent(st); 73 } catch (Exception e) { 74 LOG.log(Level.WARNING, "Problem occured while trying to shutdown in-process HSQLDB", e); 75 } finally { 76 JdbcUtils.closeSilent(con); 77 } 78 } 79 80 public void close() { 81 if (shutdownOnExit) { 82 HsqlConnection previous = Driver.setLastConnection(this); 83 if (previous != null) { 85 previous.shutdownOnExit = false; 86 previous.close(); 87 } 88 } else { 89 super.close(); 90 } 91 } 92 93 private static boolean isInprocess(String url) { 94 if (url.startsWith("jdbc:hsqldb:http:")) { 96 return false; 97 } 98 if (url.startsWith("jdbc:hsqldb:https:")) { 99 return false; 100 } 101 if (url.startsWith("jdbc:hsqldb:hsql")) { 102 return false; 103 } 104 return !url.startsWith("jdbc:hsqldb:hsqls"); 105 } 106 107 108 109 } 110 | Popular Tags |