JNDI連接池連接Oracle數據庫


今天做了一個評論的小功能,要求用JNDI連接池連接Oracle數據庫,以前只是測試了是否連接的上,現在沒想到一個JNDI連接池連接Oracle數據庫,糾結了好久,原來都是Oracle數據庫的問題,這是過失。下面介紹一下JNDI連接池連接Oracle數據庫。

JNDI介紹
什么是JNDI?
JNDI(Java Naming and Directory Interface,Java命名和目錄接口)
是一組在Java應用中訪問命名和目錄服務的API
通過名稱將資源與服務進行關聯
 
什么是連接池技術?
 
連接池
連接池是在內存中預設好一定數量的連接對象,以備用戶在進行數據庫操作時直接使用
性能
數據庫連接的建立、斷開均由管理池統一管理
連接池技術與傳統數據庫連接的比較
數據庫操作性能得到提升
通過連接池管理數據庫的連接與釋放、提高了系統資源的使用效率
 
為什么使用連接池?
Ø傳統數據庫連接方式的不足
Ø每一次請求時均需要與數據庫進行連接,資源占用較多
Ø當並發訪問數量較大時,網站速度收到極大影響
Ø在訪問結束后必須要關閉連接釋放資源
Ø系統的安全性和穩定性相對較差
 
企業級開發需要穩健和高效的數據訪問層
完成對數據庫的CRUD操作
能夠處理數據庫發生的各種錯誤
可以靈活的修改配置
提供方便使用的工具
高性能
 
訪問數據源
第一步:Tomcat的conf/context.xml中的配置
 加入數據庫驅動文件
把數據庫驅動的.jar文件,加入到Tomcat的common\lib中
應用程序的web.xml文件的配置
在web.xml中配置<resource-ref>
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

--紅色字體是需要根據數據庫名配置
<Resource name="jdbc/liuyan" auth="Container"
type="javax.sql.DataSource" maxActive="100"
maxIdle="30" maxWait="10000" username="epet" password="123456"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl"
/>
</Context>

 

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/** 
 * 文件名:JNDI.java
 * 描    述:這是一個數據庫連接類
 * 作    者:WLX
 * 所屬項目:MyNews
 * JDK版本:JDK1.7 
 * 創建時間:2015年5月23日 下午2:54:24
 *
*/
public class JNDI {

    public Connection getconn() {
        
        Connection conn = null;
        try {
            
            Context ctx;
            ctx = new InitialContext();
            //java:comp/env/為前綴
            DataSource source = (DataSource)ctx.lookup("java:comp/env/jdbc/liuyan");
        
            Connection connection = source.getConnection();
            if(connection != null){
                conn = connection;
            }
            
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM