zoukankan      html  css  js  c++  java
  • mysql连接池的使用工具类代码示例

    mysql连接池代码工具示例(scala):

    import java.sql.{Connection,PreparedStatement,ResultSet}
    import org.apache.commons.dbcp.BasicDataSource
    
    object ConnectPoolUtil{
        private var bs:BasicDataSource = null          
    
     /**
     *创建数据源
     */
     def getDataSource():BasicDataSource = {
       if (bs = null){
           bs = new BasicDataSource()
           bs.setDriverClassName("com.mysql.jdbc.Driver")
           bs.setUrl("jdbc:mysql://localhost:3306/${db_name}")
           bs.setUsername("root")
           bs.setPassword("123456")
           bs.setMaxActive(200) //设置最大的并发数
           bs.setInitialSize(30) //数据库初始化时,创建的连接个数
           bs.setMinIdle(50) //最小空闲连接数
           bs.setMaxIdle(200) //数据库最大连接数
           bs.setMaxWait(1000)
           bs.setMinEvictableIdleTimeMillis(60 * 1000) //空闲连接60秒之后释放
           bs.setTimeBetweenEvictionRunsMills(5 * 60 * 1000) //5分钟检测一次是否有进程死掉
        }  
        bs
     }
    
     /**
     *释放数据源
     */
     def shutDownDataSource(){
        if (bs != null) {
            bs.close()
        }  
     }
    
     /**
     *获取数据库的连接
     */
     def getConnection():Connection = {
         val conn :Connection = null
         try{
              if (bs != null){
                 conn = bs.getConnection()
              }else{
                  conn = getDataSource().getConnection()
              }
          }catch {
               case e:Exception => println(e.getMessage)
         }
         conn
     }
    
     /**
     *关闭连接
     */
     def closeConn(rs:ResultSet,ps:PreparedStatement,conn:Connection){
           if (rs != null)
              rs.close()
           if (ps != null)
              ps.close()
           if (conn != null)
              conn.close()
     }
    }
    

      

  • 相关阅读:
    【JavaScript】explode动画
    【JavaScript】插件参数的写法
    【webpack】理解配置文件
    你真的了解盒模型么
    一看看懂Protocol Buffer(协议篇)
    es7你都懂了吗?今天带你了解es7的神器decorator
    快速了解react
    简单聊一聊那些svg的沿路径运动
    转转RN工程化历程
    微信小程序内嵌网页的一些(最佳)实践
  • 原文地址:https://www.cnblogs.com/Gxiaobai/p/9716765.html
Copyright © 2011-2022 走看看