1 16 package scriptella.driver.hsqldb; 17 18 import scriptella.jdbc.GenericDriver; 19 import scriptella.jdbc.JdbcConnection; 20 import scriptella.spi.ConnectionParameters; 21 22 import java.sql.SQLException ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 37 public class Driver extends GenericDriver { 38 private static final Logger LOG = Logger.getLogger(Driver.class.getName()); 39 public static final String HSQLDB_DRIVER_NAME = "org.hsqldb.jdbcDriver"; 40 41 private static Map <String , HsqlConnection> lastConnections = null; private static boolean hookAdded = false; 43 44 45 48 static final Thread HOOK = new Thread ("Scriptella HSLQDB Shutdown Fix") { 49 public void run() { 50 if (lastConnections != null) { 51 for (Map.Entry <String , HsqlConnection> entry : lastConnections.entrySet()) { 52 try { 53 entry.getValue().shutdown(); 54 } catch (Exception e) { 55 LOG.log(Level.WARNING, "Problem occured while trying to shutdown an in-process HSQLDB database " + entry.getKey(), e); 56 } 57 } 58 lastConnections = null; 59 } 60 } 61 }; 62 63 64 public Driver() { 65 loadDrivers(HSQLDB_DRIVER_NAME); 66 } 67 68 @Override 69 protected JdbcConnection connect(ConnectionParameters parameters, Properties props) throws SQLException { 70 return new HsqlConnection(getConnection(parameters.getUrl(), props), parameters); 71 } 72 73 74 82 static synchronized HsqlConnection setLastConnection(HsqlConnection connection) { 83 if (lastConnections == null) { 84 lastConnections = new HashMap <String , HsqlConnection>(); 85 } 86 final HsqlConnection old = lastConnections.put(getConnectionURL(connection), connection); 87 if (!hookAdded) { 88 Runtime.getRuntime().addShutdownHook(HOOK); 89 hookAdded = true; 90 } 91 return old; 92 } 93 94 private static String getConnectionURL(HsqlConnection connection) { 95 try { 96 return connection.getNativeConnection().getMetaData().getURL(); 97 } catch (Exception e) { 98 LOG.log(Level.WARNING, "Unable to read connection meta data", e); 99 return ""; 100 } 101 } 102 103 104 } 105 | Popular Tags |