zoukankan      html  css  js  c++  java
  • JNDI使用小记

    JDNI使用小记
    
           < Resource name ="jdbc/mysql"
                    auth ="Container"   
                       type ="javax.sql.DataSource"
                       maxActive ="100"
                       maxIdle ="30"
                       maxWait ="60"   
                       wait_timeout ="18800"
                       timeBetweenEvictionRunsMillis ="300000"   
                       minEvictableIdleTimeMillis ="600000"
                       username ="root"
                       password ="root"   
                       driverClassName ="com.mysql.jdbc.Driver"   
                       url ="jdbc:mysql://localhost:3306/studentjxy?comautoReconnect=true&failOverReadOnly=false"   
                       removeAbandoned ="true"
                       removeAbandonedTimeout ="60"   
                       logAbandoned ="true" />
    
    package db;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.sql.DataSource;
    
    public class DBCache {
           private InitialContext ic ;
           private Map<String, DataSource> cache ;
           private static DBCache instance = new DBCache();
    
           private DBCache() {
                 try {
                       ic = new InitialContext();
                } catch (NamingException e) {
                      e.printStackTrace();
                }
                 cache = Collections.synchronizedMap( new HashMap<String, DataSource>());
          }
    
           public static DBCache getInstance() {
                 return instance ;
          }
    
           public Connection getDBConn(String dataSourceName) throws SQLException,
                      NamingException {
                Connection conn = null ;
                conn = getDataSource(dataSourceName).getConnection();
                 return conn;
          }
    
           public DataSource getDataSource(String dataSourceName)
                       throws NamingException {
                 if (cache .containsKey(dataSourceName)) {
                       return cache .get(dataSourceName);
                } else {
                      DataSource newDataSource = (DataSource) ic .lookup(dataSourceName);
                       cache .put(dataSourceName, newDataSource);
                       return newDataSource;
                }
          }
    
           public void removeDateSouceOffCache(String dataSourceName) {
                 try {
                      DataSource ds = cache .get(dataSourceName);
                      ds.getConnection().close();
                       cache .remove(dataSourceName);
                } catch (Exception ex) {
                }
          }
    
           public static void freeConn(Connection conn) {
                 freeConn(conn, null, null );
          }
    
           public static void freeConn(Connection connection, Statement statement) {
                 freeConn(connection, statement, null);
          }
    
           public static void freeConn(Connection conn, Statement ps, ResultSet rs) {
                 try {
                       try {
                             if (rs != null)
                                  rs.close();
                      } finally {
                             try {
                                   if (ps != null)
                                        ps.close();
                            } finally {
                                   if (conn != null)
                                        conn.close();
                            }
                      }
                } catch (SQLException ex) {
                      ex.printStackTrace();
                }
          }
    }
    
    package db;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import javax.naming.NamingException;
    
    public class DBManager {
    
           public Connection getConnection() {
    
                DBCache db = DBCache. getInstance();
                Connection conn = null ;
                 try {
                      conn = db.getDBConn( "java:comp/env/jdbc/mysql" );
                } catch (SQLException e) {
                       // TODO Auto-generated catch block
                      e.printStackTrace();
                } catch (NamingException e) {
                       // TODO Auto-generated catch block
                      e.printStackTrace();
                }
                 return conn;
          }
    
    }
    

      

  • 相关阅读:
    植物:吊兰
    植物:文竹
    理论:混沌理论
    图书-数学:《数学之美》
    调味品:味精
    linux查看文件大小df-du
    linux防火墙开启-关闭
    tomcat端口号、日志、启停
    chmod
    jdk-tomcat环境变量设置
  • 原文地址:https://www.cnblogs.com/tatame/p/2650080.html
Copyright © 2011-2022 走看看