Jndi Database Connectivity Test

Source: Internet
Author: User
Tags auth tomcat tomcat server

Recently wrote a Web project found that only using the JDBC database, the "Pre-logon" error always occurs because the number of database connections is limited, so using the Jndi connection database solves the problem, and a database connection pool is created through Jndi. Multiple database connections have been established in DataSource, and these database connections are stored in connection pools (connect pool). When a Java program accesses a database, it only needs to remove the idle state of the database connection from the connection pool, and then put the database connection back into the connection pool when the program accesses the database.

Here's how to configure:

1, CONFIGURED in Tomcat:

A.>tomcat installation directory of the context.xml of the Conf

<Context></Context> in

<resource name= "jdbc/db_net" auth= "Container" type= "Javax.sql.DataSource" maxactive= "10"
Maxidel= "maxwait=" "1000" username= "Lifan" password= "sqllifan2005"
Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
Url= "Jdbc:sqlserver://localhost:1434;database=db_net" >
</Resource>

which

Name indicates the JNDI name specified

Auth means the authentication method, generally is container

Type represents the data source bed type, using the standard Javax.sql.DataSource

Maxactive represents the largest database connection in the connection pool

Maxidle represents the maximum number of idle connections

Maxwait when the pool's database connection is already occupied, the maximum wait time

Username represents the database user name

Password represents the password of the database user

Driverclassname represents JDBC DRIVER

URL represents the database URL address

B.> copies the appropriate JAR packages to the Lib directory under the Tomcat server, with the jar package name as follows

Commons-dbcp.jar

Commons-pool.jar

Commons-collections-2.1.jar

C.> gets the code for the Connection object as follows

Import Javax.naming.Context;
Import Javax.naming.InitialContext;
Import Javax.sql.DataSource;

private static Connection CN = NULL;

/*
* Get Connection object
*/
private void getconnection () {
try {
Context context = new InitialContext ();
DataSource ds = (DataSource) context.lookup ("Java:/comp/env/jdbc/db_net");//jdbc/db_net is the value of the above Name property
cn = Ds.getconnection ();
catch (Exception e) {
E.printstacktrace ();
}
}

2. But if you have multiple apps under Tomcat and want to configure the Jndi configuration for a particular project, you should create context.xml files under the Meta-int directory under Webroot, and add a statement to the content above factory = "Org.apache.commons.dbcp.BasicDataSourceFactory"

At the same time, copy the jar package above to the Lib directory under Web-info under Webroot

The code to get the connection object remains unchanged

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.