Adding Jndi data source information to Tomcat dynamically

Source: Internet
Author: User

When we develop the project, we always have to deal with the database, how to get the data source, in what way to obtain, become we are simple and familiar, and should not pay attention to a problem.

Well, here I am. Three common ways to get data sources: first, through the configuration file to get
    1. First, under the classpath of the project, create a configuration file, such as Jdbc.properties.
    2. In the most primitive way, the configuration file is parsed, then the configuration information is read and the JDBC information is encapsulated to get the data source.
    3. Use spring to build datasource beans through spring. The information in this DataSource Bean also comes to you in your config file. Note to include in the configuration file:
<location= "Classpath:jdbc.properties"/>
Introducing static files

The datasource beans in 4.Spring are roughly as follows:

<BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close">        < Propertyname= "Driverclassname"value= "${driverclassname}" />        < Propertyname= "url"value= "${url}" />        < Propertyname= "username"value= "${username}" />        < Propertyname= "Password"value= "${password}" />        < Propertyname= "Maxactive"value= " the" />        < Propertyname= "InitialSize"value= "1" />        < Propertyname= "Maxwait"value= "60000" />        < Propertyname= "Maxidle"value= " the" />        < Propertyname= "Minidle"value= "3" />        < Propertyname= "removeabandoned"value= "true" />        < Propertyname= "Removeabandonedtimeout"value= " the" />        < Propertyname= "ConnectionProperties"value= "Clientencoding=utf-8" /></Bean>
DataSource Bean

5. The contents of the configuration file are as follows:

Driverclassname=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/springsecurity ? allowmultiqueries=trueusername=rootpassword=123456
JDBC Configuration file
Second, through Jndi to obtain
    1. Description: Fetching through JNDI, usually in the case of using spring, can be otherwise.
    2. First, in the Context.xml file under Tomcat's config directory, add the following statement:
<resource driverclassname= "Com.mysql.jdbc.Driver" maxactive= "+" maxwait= "" maxldle= "" Name= "app" Type= " Javax.sql.DataSource "url=" Jdbc:mysql://127.0.0.1:3306/goldapp?allowmultiqueries=true "username=" root "password=" 123456 "/>
Jndi Resoure

3. For the description of the "2" point: Name is the name of Jndi, Driverclassname is the driver class used, the URL is of course connection related information, username is the user name of the login database, password is the database password.

4. Do what you need to do after the "2" Step: In your Spring configuration file, add the bean that introduces Jndi, as follows:

class= "Org.springframework.jndi.JndiObjectFactoryBean" >          <property name= "Jndiname" >              <value>java:comp/env/app</value>          </property>  </bean>
Jndi Spring Bean

5. For the description of the "4" point:

java:comp/env/is the prefix in value, and the app help is the name of the Jndi configured in Content.xml.

Third, add the Content.xml mode under the Mata-info file, and let the project dynamically add Jndi data source to tomcat at deployment time.

1. First, build the file under Mata-info: Context.xml

The contents of the file are as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Context>    <ResourceDriverclassname= "Com.mysql.jdbc.Driver"maxactive= "+"maxwait= "the"Maxldle= "Ten"name= "App"type= "Javax.sql.DataSource"URL= "Jdbc:mysql://192.168.10.105:3306/springsecurity?allowmultiqueries=true"username= "root"Password= "123456"    /></Context>
Mata-info Context Content

2. Description: After adding this file under Mata-info, reference the database in spring using Jndi. The advantage of this is. It will dynamically add data source information to your Tomcat profile when you publish the project, so you don't have to manually add the configuration to the Content.xml file each time. Note: The dynamically added data source information is in Servce.xml.

Adding Jndi data source information to Tomcat dynamically

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.