Proxool数据库连接池
1、web.xml中配置监听器
1 <listener> 2 <listener-class>com.richinfo.listener.InitListener</listener-class> 3 </listener> 4 <context-param> 5 <param-name>webAppRootKey</param-name> 6 <param-value>webApp.root</param-value> 7 </context-param>
2、监听器InitListener
1 public class InitListener implements ServletContextListener { 2 private Logger logger = null; 3 @Override 4 public void contextDestroyed(ServletContextEvent arg0) { 5 destoryPool();// 释放memcached连接池 6 } 7 8 @Override 9 public void contextInitialized(ServletContextEvent arg0) { 10 String PFSApiFilePath=""; 11 logger = LogManager.getLogger(InitListener.class.getName()); 12 logger.info("-----initMemcachePool......-----"); 13 //增加定时任务 14 addJob("cacheCleanJob","cacheCleanGroup",ContentService.cronExpression ,new CacheCleanTask()); 15 addJob("userTaskJob","userTaskGroup",ContentService.cronUserExpression ,new UserTask()); 16 try { 17 PFSApiFilePath=arg0.getServletContext().getRealPath("/")+"WEB-INF" +File.separator+"classes" + File.separator +"PFSApiConfig.xml"; 18 System.out.println("PFSApiFilePath:" +PFSApiFilePath); 19 ReadXMLConfig.readXMLFile(PFSApiFilePath); 20 logger.info("-----init FPS readXMLFile is success"); 21 } catch (Exception e) { 22 logger.info("-----init FPS readXMLFile is fail:"+"|path:"+ PFSApiFilePath); 23 e.printStackTrace(); 24 } 25 26 logger.info("-----init success......-----"); 27 String img = "http://ww3.sinaimg.cn/crop.0.53.432.324.240/005M94J9jw1ezrqz0z0qxj30c00ewglu.jpg"; 28 } 29 30 //定时器 31 public void addJob(String jobName, String groupName, String cronExpression, Job job) { 32 SchedulerFactory sfw = new StdSchedulerFactory(); 33 try { 34 Scheduler sched = sfw.getScheduler(); 35 JobDetail jobDetail = new JobDetail(jobName, groupName, job.getClass());// 任务名,任务组,任务执行类 36 CronTrigger trigger = new CronTrigger(jobName, cronExpression);// 触发器名,触发器组 37 trigger.setCronExpression(cronExpression);// 触发器时间设定 38 trigger.setTimeZone(TimeZone.getTimeZone("GMT+8")); 39 sched.scheduleJob(jobDetail, trigger); 40 // 启动 41 if (!sched.isShutdown()) 42 sched.start(); 43 } catch (SchedulerException e) 44 { 45 46 } catch (ParseException e) { 47 48 } 49 } 50 51 private void destoryPool() { 52 //SockIOPool.getInstance("SockIOPool_UPM").shutDown(); 53 RedisCached.destroy(); 54 logger.info("释放memcached pool完成!"); 55 } 56 57 58 }
3、定时任务CacheCleanTask
1 public class CacheCleanTask implements Job { 2 @Override 3 public void execute(JobExecutionContext arg0) throws JobExecutionException { 4 // 清理MemCached缓存前取出部分缓存信息,并重置缓存时间信息 5 String strDate = RedisCached.get("UPMMemCachedIsDayRead"); 6 boolean issucc = false; 7 long begin = System.currentTimeMillis(); 8 9 if (strDate != null && !"".equals(strDate)) { 10 Date DayReadDate = new Date(); 11 long DateDiffTimeSpan = 0; 12 DayReadDate = DateTimeTools.stringToDateTime(strDate, "yyyy-MM-dd HH:mm:ss"); 13 logInfo += "UPMMemCachedIsDayRead The last time:" + strDate + "|"; 14 DateDiffTimeSpan = DateTimeTools.DateDiffTimeSpan(DayReadDate, new Date(), 4); 15 logInfo += " DateDiffTimeSpan :" + DateDiffTimeSpan + "M|"; 16 RedisCached.set("UPMMemCachedIsDayRead", strDate, (int) DateDiffTimeSpan); 17 } 18 log.info("每日清理缓存成功," + issucc); 19 // 清理所有Redis缓存 20 issucc = RedisCached.flushAll(); 21 //刷新所有位置缓存(刷新全国/省/市地区信息) 22 loadCache2(); 23 //将已读到用户和内容的对应关系存储到MemCached中,使用户能优先显示未读的内容 24 loadMemUserIsReadCache(); 25 //刷新 数据源缓存 26 LoadDATASOURCECache2(); 27 //刷新所有位置所有地区的静态文件缓存(本地缓存) 28 LoadStaticFile(); 29 30 // 可以考虑不刷新所有缓存 31 ContentService.MAP_CONTENT_SHOW_RULE.clear(); 32 ContentService.MAP_CONTENT_SHOW_RULE_TAG.clear(); 33 34 log.info("[CacheCleanTask]total cost:{} ms.", System.currentTimeMillis() - begin); 35 } 36 }
4、Proxool.xml连接池配置
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 <!-- the proxool configuration can be embedded within your own application's. 3 Anything outside the "proxool" tag is ignored. --> 4 <something-else-entirely> 5 <proxool> 6 <alias>public</alias> 7 <driver-url>jdbc:oracle:thin:@192.168.9.73:1521:oss</driver-url> 8 <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> 9 <driver-properties> 10 <property name="user" value="upm"/> 11 <property name="password" value="upm"/> 12 </driver-properties> 13 <maximum-connection-lifetime>600000</maximum-connection-lifetime> 14 <maximum-connection-count>5</maximum-connection-count> 15 <house-keeping-test-sql>select CURRENT_DATE FROM dual</house-keeping-test-sql> 16 </proxool> 17 </something-else-entirely>
5、连接池管理工具类
1 package com.richinfo.dao.base; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import org.apache.logging.log4j.LogManager; 10 import org.apache.logging.log4j.Logger; 11 12 public class DBManager { 13 private static Logger log = LogManager.getLogger(DBManager.class); 14 15 /** 16 * 默认的数据库链接,链接Oss139 统一位置 管理下的对象 17 * 18 * @return Connection 19 */ 20 public static Connection getConnection() { 21 22 try { 23 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 24 } catch (ClassNotFoundException e) { 25 e.printStackTrace(); 26 return null; 27 } catch (Exception e) { 28 e.printStackTrace(); 29 return null; 30 } 31 Connection conn = null; 32 try { 33 conn = DriverManager.getConnection("proxool.public"); 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 log.error("DBManager getConnection null.{}", e); 37 return null; 38 } 39 return conn; 40 } 41 42 /** 43 * 链接指定的 数据库对象 44 * 45 * @param dataBaseName 46 * 对象名 47 * @return Connection 48 */ 49 public static Connection getConnection(String dataBaseName) { 50 51 try { 52 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 53 } catch (ClassNotFoundException e) { 54 e.printStackTrace(); 55 return null; 56 } catch (Exception e) { 57 e.printStackTrace(); 58 return null; 59 } 60 Connection conn = null; 61 try { 62 conn = DriverManager.getConnection("proxool." + dataBaseName); 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 return null; 66 } 67 return conn; 68 } 69 70 public static void close(Connection conn, Statement st, ResultSet rs) { 71 72 try { 73 if (rs != null) 74 rs.close(); 75 } catch (Exception e) { 76 log.error("DBManager close fail rs. {}", e); 77 } 78 ; 79 try { 80 if (st != null) 81 st.close(); 82 } catch (Exception e) { 83 log.error("DBManager close fail st. {}", e); 84 } 85 ; 86 try { 87 if (conn != null) 88 conn.close(); 89 } catch (Exception e) { 90 log.error("DBManager close fail conn. {}", e); 91 } 92 } 93 }