Connect the JNDI connection pool to the Oracle database and the jndioracle Database

Source: Internet
Author: User

Connect the JNDI connection pool to the Oracle database and the jndioracle Database

Today, I made a comment on the small function and asked to use the JNDI connection pool to connect to the Oracle database. I used to test whether to connect to the Oracle database. Now I did not expect a JNDI connection pool to connect to the Oracle database. It has been a long struggle, it turns out to be an Oracle database problem. This is a fault. The following describes how to connect a JNDI connection pool to an Oracle database.

What is JNDI? JNDI (Java Naming and Directory Interface, Java Naming and Directory Interface) A group of APIs that access naming and directory services in Java applications associate resources with services by name. What is connection pool technology? The connection pool pre-sets a certain number of connection objects in the memory, in case that you can directly use the performance database for database operations, the establishment and disconnection of database connections are all managed by the management pool, and the connection pool technology is compared with traditional databases. The database operation performance is improved by using the connection pool to manage databases. connection and release improve the efficiency of using system resources. Why use the connection pool? Traditional database connection methods are insufficient. Every request must be connected to the database, occupying a large amount of resources. When the number of concurrent accesses is large, the website speed has been greatly affected. En connection must be closed after access to release resources. En the system security and stability are relatively poor. enterprise-level development requires a stable and efficient data access layer to complete CRUD operations on the database. to handle various errors in the database, you can flexibly modify configurations and provide easy-to-use tools for High-Performance Data Source Access. Step 1: conf/context of Tomcat. add the configuration in xml to the database driver file to drive the database. jar file, which is added to the web of the Tomcat common \ lib application. the xml file is configured on the web. configure <resource-ref> in xml
<? 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 License t in compliance with the License. you may obtain a copy of the License 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 resume SS 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 "/> -- the red font indicates that the <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;/*** file name: JNDI. java * Description: This is a database connection class * Author: WLX * Project: MyNews * JDK version: JDK1.7 * Creation Time: may 23, 2015 2:54:24 **/public class JNDI {public Connection getconn () {Connection conn = null; try {Context ctx; ctx = new initi1c Ontext (); // java: comp/env/is prefixed with 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 ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.