Dynamically add JNDI data source information for Tomcat
Dynamically add JNDI data source information for Tomcat
When developing projects, we always have to deal with databases, how to obtain data sources, and how to obtain them have become a simple, familiar and important issue.
Here I will talk about three common methods to obtain data sources:
1. Get it through the configuration file
1. First, create a configuration file, such as jdbc. properties, under the classPath of the project.
2. parse the configuration file, read the configuration information, and encapsulate the jdbc information to obtain the data source.
3. Use spring; use spring to create dataSource Bean. The information in this dataSource Bean is also in your configuration file. Note:
<Context: property-placeholder location = "classpath: jdbc. properties"/>
1. dataSource bean in Spring is roughly as follows:
<Bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource"
Destroy-method = "close">
<Property name = "driverClassName" value = "$ {driverClassName}"/>
<Property name = "url" value = "$ {url}"/>
<Property name = "username" value = "$ {username}"/>
<Property name = "password" value = "$ {password}"/>
<Property name = "maxActive" value = "20"/>
<Property name = "initialSize" value = "1"/>
<Property name = "maxWait" value = "60000"/>
<Property name = "maxIdle" value = "20"/>
<Property name = "minIdle" value = "3"/>
<Property name = "removeAbandoned" value = "true"/>
<Property name = "removeAbandonedTimeout" value = "180"/>
<Property name = "connectionProperties" value = "clientEncoding = UTF-8"/>
</Bean>
5. The content of the configuration file is roughly as follows:
DriverClassName = com. mysql. jdbc. Driver
Url = jdbc: mysql: // localhost: 3306/springsecurity? AllowMultiQueries = true
Username = root
Password = 123456
2. Obtain through jndi
1. Description: It is obtained through the jndi method. It is usually used when spring is used. Of course it can be another method.
2. First, add the following statement to the context. xml file under the config directory of tomcat:
<Resource driverClassName = "com. mysql. jdbc. driver "maxActive =" 120 "maxWait =" 5000 "maxldle =" 10 "name =" app "type =" javax. SQL. dataSource "url =" jdbc: mysql: // 127.0.0.1: 3306/goldApp? AllowMultiQueries = true "username =" root "password =" 123456 "/>
3. description of "2": name indicates the jndi name, driverClassName indicates the driver class used, and url indicates the connection information. username indicates the username used to log on to the database, password is the database password.
4. After step 2 is completed, add the bean that introduces jndi to your spring configuration file, as shown below:
5. Description of "4:
In value, java: comp/env/is the prefix, and app is the jndi name configured in content. xml.
3. Add the Content. xml method to the MATA-INFO file to allow the project to dynamically Add the jndi data source to tomcat during deployment
1. First, create the file under the MATA-INFO: Context. xml
The file content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Context>
<Resource
DriverClassName = "com. mysql. jdbc. Driver"
MaxActive = "120"
MaxWait = "5000"
Maxldle = "10"
Name = "app"
Type = "javax. SQL. DataSource"
Url = "jdbc: mysql: // 192.168.10.105: 3306/springsecurity? AllowMultiQueries = true"
Username = "root"
Password = "123456"
/>
</Context>
2. Note: After adding this file to mata-info, use the jndi method to reference the database in spring. The benefit is. When you release a project, it dynamically adds the data source information to the tomcat configuration file, so we do not need to manually add the configuration to the content. xml file every time. Note: The dynamically added data source information is in servce. xml.
Tomcat6 adds the MySQL JNDI Data Source
Install JDK + Tomcat in RedHat Linux 5.5 and deploy Java Projects
Tomcat authoritative guide (second edition) (Chinese/English hd pdf + bookmarks)
Tomcat Security Configuration and Performance Optimization
How to Use Xshell to view Tomcat real-time logs with Chinese garbled characters in Linux
Install JDK and Tomcat in CentOS 64-bit and set the Tomcat Startup Procedure
Install Tomcat in CentOS 6.5
Tomcat details: click here
Tomcat: click here
This article permanently updates the link address: