|
|
DBConnectionPool.java1.package com.billows.util; 2. 3.import java.sql.Connection; 4. 5.import javax.naming.Context; 6.import javax.naming.InitialContext; 7.import javax.sql.DataSource; 8. 9.public class DBConnectionPool { 10. public static Connection getConnection() throws Exception{ 11. Context ctx = new InitialContext(); 12. DataSource ds = (DataSource)ctx.lookup("java:comp/env/hsql"); 13.// Context initContext = new InitialContext(); 14.// Context envContext = (Context)initContext.lookup("java:/comp/env"); 15.// DataSource ds = (DataSource)envContext.lookup("hsql"); 16. return ds.getConnection( ); 17. } 18.}
hsqldb随web启动的Listener:
1.package com.billows.util; 2. 3.import java.sql.Connection; 4.import java.sql.DriverManager; 5.import java.sql.SQLException; 6.import java.sql.Statement; 7. 8.import javax.servlet.ServletContextEvent; 9.import javax.servlet.ServletContextListener; 10. 11.import org.apache.log4j.Logger; 12.import org.hsqldb.Server; 13. 14.import com.billows.util.EnvironmentConfig; 15./** 16. * 该listener随web服务启动 并同时启动hsql-server 17. * @auth Billows.Van 18. * billows.van@gmail.com 19. */ 20.public class HsqlListener implements ServletContextListener { 21. public static final Logger log = Logger.getLogger("HsqlListener"); 22. private EnvironmentConfig ec=EnvironmentConfig.getInstance(); 23. /**配置文件中的占位符,代表webapp发布后的根目录.*/ 24. public static final String TOKEN = "${webapp.root}"; 25. /** 等待数据库停止的最大时间.*/ 26. public static final int WAIT_TIME = 1000; 27. /**jdbc的url.*/ 28. private String url; 29. /**登陆用户名.*/ 30. private String username; 31. /**登陆密码.*/ 32. private String password; 33. /**处理context初始化事件. 34. * @param sce ServletContextEvent 35. */ 36. public void contextInitialized(ServletContextEvent sce) { 37. try { 38. username = ec.getPropertyValue("/config/jdbc.properties", "jdbc.username"); 39. password = ec.getPropertyValue("/config/jdbc.properties", "jdbc.password"); 40. String databaseName = ec.getPropertyValue("/config/jdbc.properties", "jdbc.dbname"); 41. int port = Integer.parseInt(ec.getPropertyValue("/config/jdbc.properties", "jdbc.dbport")); 42. String hsqlPath = ec.getPropertyValue("/config/jdbc.properties", "jdbc.dbpath"); 43. 44. // FIXME: 因为要用到getRealPath方法获得路径,在使用war包发布的时候会出现问题 45. if (hsqlPath.startsWith(TOKEN)) { 46. String webappRoot = sce.getServletContext().getRealPath("/").replace("\\", "/"); 47. hsqlPath = hsqlPath.substring(TOKEN.length()); 48. hsqlPath = webappRoot + hsqlPath; 49. } 50. String databasePath = hsqlPath + "/" + databaseName; 51. url = "jdbc:hsqldb:hsql://localhost:" + port + "/" + databaseName; 52. Server server = new Server(); 53. server.setDatabaseName(0, databaseName); 54. server.setDatabasePath(0, databasePath); 55. server.setPort(port); 56. server.setSilent(true); 57. server.start(); 58. Thread.sleep(WAIT_TIME); 59. log.info("Hsqldb启动成功!"); 60. } catch (Exception ex) { 61. log.error("Hsqldb启动失败:" + ex); 62. } 63. } 64. 65. /** 66. * 处理context销毁事件. 67. * @param sce ServletContextEvent 68. */ 69. public void contextDestroyed(ServletContextEvent sce) { 70. try { 71. Class.forName("org.hsqldb.jdbcDriver"); 72. Connection conn = null; 73. Statement state = null; 74. try { 75. // 向数据库发送shutdown命令,关闭数据库 76. conn = DriverManager.getConnection(url, username, password); 77. state = conn.createStatement(); 78. state.executeUpdate("SHUTDOWN;"); 79. try { 80. Thread.sleep(WAIT_TIME); 81. } catch (InterruptedException e) { 82. e.printStackTrace(); 83. } 84. log.info("关闭hsqldb数据库成功!"); 85. } catch (SQLException ex1) { 86. log.error("关闭hsqldb数据库时出现异常:" + ex1); 87. } finally { 88. // 确保关闭Statement 89. if (state != null) { 90. try { 91. state.close(); 92. state = null; 93. } catch (SQLException ex1) { 94. log.error("关闭Statement时异常:"+ex1); 95. } 96. } 97. // 确保关闭Connection 98. if (conn != null) { 99. try { 100. conn.close(); 101. conn = null; 102. } catch (SQLException ex1) { 103. log.error("关闭Connection时异常:"+ex1); 104. } 105. } 106. } 107. } catch (ClassNotFoundException ex) { 108. log.error("HsqldbListener : contextDestoryed : error : " + ex); 109. } 110. } 111.}
其中, jdbc.properties内容为:
1.jdbc.driverClassName=org.hsqldb.jdbcDriver 2.jdbc.url=jdbc:hsqldb:hsql://localhost:9990/billows 3.jdbc.dbname=billows 4.jdbc.dbport=9990 5.jdbc.dbpath=${webapp.root}/hsqldb 6.jdbc.username=sa 7.jdbc.password= 8.jdbc.max_connections=30
在web.xml中添加以下代码即可
1.<listener> 2. <listener-class>com.billows.util.HsqlListener</listener-class> 3.</listener> |
|