JAVA tomcat 配置jndi而且使用c3p0连接池

配置MYSQL 的JNDI采取的方式时再META-INF文件夹下创建context.xml配置应用的jndi。优点每个应用独立JNDI.

配置文件及其步骤如下:

1、META-INF中创建<context.xml内容如下: 

 <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
        name="jdbc/mysql"
        auth="Container"
        factory="org.apache.naming.factory.BeanFactory"
        type="com.mchange.v2.c3p0.ComboPooledDataSource"
        driverClass="com.mysql.jdbc.Driver"
        idleConnectionTestPeriod="60"
        acquireRetryAttempts="3"
        checkoutTimeout="10000"
        maxPoolSize="50"
        minPoolSize="2"
        acquireIncrement="2"
        user="root"
        password="root"
        jdbcUrl="jdbc:mysql://127.0.0.1:3306/ccc?useUnicode=true&characterEncoding=utf-8"/>
</Context>




 2、WEB-INF中web.xml文件中添加引用: 

<!--Oracle数据库JNDI数据源引用 -->
<!--<resource-ref>-->
    <!--<description>Oracle DB Connection</description>-->
    <!--<res-ref-name>oracleDataSource</res-ref-name>-->
    <!--<res-type>javax.sql.DataSource</res-type>-->
    <!--<res-auth>Container</res-auth>-->
<!--</resource-ref>-->

<!--MySQL数据库JNDI数据源引用 -->
<resource-ref>
    <description>MySQL DB Connection</description>
    <res-ref-name>jdbc/mysql</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<!--SQLServer数据库JNDI数据源引用 -->
<!--<resource-ref>-->
    <!--<description>SQLServer DB Connection</description>-->
    <!--<res-ref-name>sqlserverDataSource</res-ref-name>-->
    <!--<res-type>javax.sql.DataSource</res-type>-->
    <!--<res-auth>Container</res-auth>-->
<!--</resource-ref>-->
3、DAO(一个servlet或者专门的数据接入类)中读取配置及其创建connection和DataSource

import sun.jdbc.odbc.ee.ConnectionPool;
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
import java.sql.*;
import java.sql.SQLException;
import java.util.Hashtable;

/**
 * Created by wuxituong on 2016-10-01.
 */
public class SQLDAO {
    private static ConnectionPool instace;
    private static <pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><span style="background-color:#e4e4ff;">ComboPooledDataSource</span>
ds; //获得数据源 private static <span style="font-family: Arial, Helvetica, sans-serif;">
ComboPooledDataSource
</span><span style="font-family: Arial, Helvetica, sans-serif;">createDataSource()</span> { if (ds == null) { System.out.println("ds created"); try { Context ct = new InitialContext(); if (ct == null) System.out.println("无配置环境"); Context envContext = (Context) ct.lookup("java:/comp/env"); ds = (<span style="font-family: Arial, Helvetica, sans-serif;">
ComboPooledDataSource
</span><span style="font-family: Arial, Helvetica, sans-serif;">) envContext.lookup("jdbc/mysql"); //根据名称取得数据源</span> } catch (NamingException e) { e.printStackTrace(); } } return ds; } //从连接池中取得连接对象 public static synchronized Connection getConnection() throws SQLException,NamingException{ Connection con=null; try { System.out.print("正准备获取con"); //获取连接 con=(Connection)createDataSource().getConnection(); } catch (Exception e) { e.printStackTrace(); System.out.print("获取连接失败!"); } return con; } //释放连接 public static synchronized void freeConnection(Connection con){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); System.out.println("关闭连接失败!"); } }}
 4、使用DAO 记得在使用了con一定要con.close()将连接放回连接池,否则则会造成连接被占用完成,无法再获取con。导致服务器必须重启释放con 

 public ResultSet getResultSetData(String sql){
        ResultSet rs= null;
        try {
            Statement sta =  con.createStatement();
            rs = sta.executeQuery(sql);
            return  rs;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return rs;
        }
    }
    public int exeSql(String sql){
        try {
            Statement sta =  con.createStatement();
            return  sta.executeUpdate(sql);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return -1;
        }
    }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值