Today made a comment on the small function, requires a JNDI connection pool connection to the Oracle database, previously just tested the connection, and now do not think of a jndi connection pool connected to the Oracle database, tangled for a long time, the original is the Oracle database problem, this is the fault. The following describes the Jndi connection pool connection to the Oracle database.
Jndi IntroductionWhat is Jndi? JNDI (Java naming and directory Interface,java naming and directory interfaces)is a set of APIs that access naming and directory services in Java applicationsassociating a resource with a service by nameWhat is connection pooling technology? Connection PoolA connection pool is a certain number of connection objects that are preset in memory for the user to use directly in the database operationPerformancethe establishment and disconnection of database connections are managed centrally by the management poolComparison of connection pooling technology and traditional database connectionImproved database operation performanceConnecting and freeing the database through connection pooling, improving the efficiency of system resource utilizationWhy use connection pooling? Ø The shortage of traditional database connection modeØ Each request needs to connect with the database, the resource occupies moreØ When the number of concurrent visits is large, website speed is greatly affectedØ You must close the connection to release resources after the end of the visitØ The safety and stability of the system is relatively poorEnterprise-class development requires a robust and efficient data access layercomplete CRUD Operations on the databaseability to handle various errors that occur in a databaseflexibility to modify configurationseasy-to-use tools availableHigh PerformanceAccessing data sourcesFirst step: Configuration in Tomcat's Conf/context.xmladding database driver filesAdd a database-driven. jar file to the common\lib of Tomcatconfiguration of the application's Web. xml fileconfiguring <resource-ref> in Web. 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 ThisWork foradditional information regarding copyright ownership. The ASF licenses ThisFile to under the Apache License, Version 2.0( the"License"); If you don't use Thisfile except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0unless required by applicable law or agreed to writing, software distributed under the License are distributed on an"As is"BASIS, without warranties or CONDITIONS of any KIND, either express or implied. See the License forThe specific language governing permissions and limitations under the License.--><!--the contents of ThisFile would be loaded forEach Web application--><context> <!--Default set of monitored resources--<WatchedResource> Web-inf/web.xml</watchedresource> <!--Uncomment ThisTo disable session persistence across Tomcat restarts-<!--<manager pathname= ""/>-< !--Uncomment ThisTo enable Comet connection tacking (provides events in session expiration as well as WebApp lifecycle)-<!--<valve classname= "org.apache.catalina.valves.CometConnectionManagerValve"/>-->--red font is required Configuration based on database name<resource name= "Jdbc/liuyan" auth= "Container" type= "Javax.sql.DataSource" maxactive= "" "maxidle=" 30 " Maxwait= "10000" username= "Epet" password= "123456" driverclassname= "Oracle.jdbc.driver.OracleDriver" url= "Jdbc:o Racle:thin: @localhost: 1521:orcl "/></Context>
Importjava.sql.Connection;Importjava.sql.SQLException;ImportJavax.naming.Context;ImportJavax.naming.InitialContext;Importjavax.naming.NamingException;ImportJavax.sql.DataSource;/*** File Name: Jndi.java * Description: This is a database connection class * Author: WLX * belongs to project: Mynews * JDK version: JDK1.7 * created: May 23, 2015 afternoon 2:54:24 **/ Public classJNDI { PublicConnection Getconn () {Connection conn=NULL; Try{Context ctx; CTX=NewInitialContext (); //java:comp/env/as prefixDataSource 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 (); } returnConn; }}
Jndi connection pooling connection to Oracle database