Configure the JNDI data source for Tomcat

Source: Internet
Author: User

This article describes how to configure the JNDI data source in Tomcat, including configuring local data sources (only for a single application) and global data sources (all applications using this Tomcat can use this)

I. Thinking

Since we learned web development, we have implemented database access through program code. From the very beginning, I wrote the database configuration information in the code, and then I extracted the configuration information and wrote it in the properties file. The code for accessing the database is simpler.

However, access to the database through the JNDI method is more direct. The connection to the database through the JNDI method directly stores the database information in Tomcat, and the project code can directly obtain the data source through the JNDI technology. They are linked by defining the data source name in Tomcat.

It should be said that the configuration data source in JNDI mode separates the project code from the implementation deployment, which facilitates developers and implementation deployment personnel to perform their respective duties.

Ii. configuration method

Generally, local data sources are directly configured in the <context> tag of the project, as follows:

<Context docBase="S:\Workspaces\ServletProject\WebRoot" reloadable="true" >    <Resource name="jdbc/dstest" auth="Container" type="javax.sql.DataSource" maxActive="5" maxIdle="2" maxWait="10000" username="root" password="root" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:dbName"/></Context>

Note: Depending on the project configuration method, it can be configured in servlet. xml or an independent XML file under CONF/Catalina/localhost.

For a global data source, you must add the <resource> data source in the <globalnamingresources> label, then, use <resourcelink> to reference the corresponding <resource> tag in the <context> tag of the project deployment, as shown below:

1 <! -- Add a data source to globalnamingresources --> 2 <globalnamingresources> 3 <! -- Omit other --> 4 <Resource Name = "JDBC/dstest" auth = "Container" type = "javax. SQL. datasource "maxactive =" 5 "maxidle =" 2 "maxwait =" 10000 "username =" root "Password =" root "driverclassname =" oracle. JDBC. driver. oracledriver "url =" JDBC: oracle: thin: @ 127.0.0.1: 1521: dbname "/> 5 </globalnamingresources> 6 7 <! -- Reference the data source in the context of Project deployment --> 8 <context docbase = "s: \ workspaces \ servletproject \ webroot "Path =" "> 9 <resourcelink name =" JDBC/dstest "Global =" JDBC/dstest "type =" javax. SQL. datasource "/> 10 </context>

Note: 1. The name attribute of row 4th and row 9th must be the same.

2. After the global data source is configured, you can not only reference it in the server. xml file, but also reference it in the XML file in CONF/Catalina/localhost.

Selecting a local data source or a global data source depends on your own needs. If you need to use the same data source for several projects, you can use the global data source, reference the content in the <context> label of the project to avoid repeated <resource> content writing.

Iii. Test

1. Configure the data source in any way written in step 3.

2. After configuring the data source, you need to put the corresponding database driver file in the project's lib directory or Tomcat's lib directory.

3. Next, we will write a simple bean for database connection in bean. We will call this bean On the JSP page and output the result to check whether the data source is configured successfully.

Jndibean. Java File

Package COM. chanshuyi. test; import Java. SQL. connection; import Java. SQL. resultset; import Java. SQL. statement; import javax. naming. context; import javax. naming. initialcontext; import javax. SQL. datasource; public class jndibean {Public String getinfobyjndi () {string result = ""; try {context CTX = new initialcontext ();/* Name of the data source defined in the XML file: JDBC/dstest */datasource DS = (datasource) CTX. lookup ("Java: COMP/ENV/jdbc/dstest"); connection conn = Ds. getconnection (); statement stmt = Conn. createstatement (); resultset rs = stmt.exe cutequery ("select * From t_yw_product where trueid = '000000'"); While (RS. next () {result + = Rs. getstring ("finaluser") + "<br/>" ;}} catch (exception e) {e. printstacktrace ();} return result ;}}

Jnditest. jsp file:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><jsp:useBean id="jndiBean" class="com.chanshuyi.test.JNDIBean"></jsp:useBean><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

 

Configure the JNDI data source for Tomcat

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.