天天看点

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;
        }
    }