three ways to configure Jndi data sources in TomcatCategory: Java advanced 2012-07-01 10:24 18328 people read reviews (0) favorite reports Tomcatjdbcweblogicmysqlbean Test
Directory (?) [+]
In the course of my past work, the development server was generally tomcat
Data sources are often configured with a DataSource bean in Applicationcontext.xml
Then modify the Jndi configuration at deployment time
I guess that's because Tomcat configuration needs to be changed to config file
Unlike servers like Jboss,weblogic, you can add Jndi data sources directly from the admin interface
And few people are going to study its configuration.
Recently made a small project, the release of the release is through the ant compiled into a jar package and then dropped to the test
The test is uncle, teach him to change the data source or when not heard
Weekend bored, read some of the Tomcat configuration tutorial, below to do some summary
Note: If your project is lost directly under WebApps, there is no project corresponding to the context node in Server.xml
Update: Since some of the previous configuration from the network, not very good, made some updates
Made a few personal comments on each of these methods.
PS: The following configuration is tested under apache-tomcat-6.0.35 and can access the database
First, a single application with exclusive data sources is one step, find Tomcat Server.xml find the project context node, add a private data source XML code
- <Context docbase="WebApp" path="/webapp" reloadable="true" source=" Org.eclipse.jst.jee.server:WebApp ">
- <Resource
- name="Jdbc/mysql"
- scope="shareable"
- type="Javax.sql.DataSource"
- factory="Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- url="Jdbc:mysql://localhost:3306/test"
- driverclassname ="Com.mysql.jdbc.Driver"
- username="root"
- password="root"
- />
- </Context>
Advantages: Simple
Cons: Poor reusability
Second, configure the global Jndi data source to apply to a single application
In two steps
The first step is to find the Globalnamingresources node in Tomcat's Server.xml and add a global data source XML code under the node
- <Resource
- name="Jdbc/mysql"
- scope="shareable"
- type="Javax.sql.DataSource"
- factory="Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
- url="Jdbc:mysql://localhost:3306/test"
- driverclassname ="Com.mysql.jdbc.Driver"
- username="root"
- password="root"
- />
Second, find the project context node to which you want to apply this Jndi data source, and increase the reference to the global data source ResourceLink XML code
- <context docbase= "WebApp" path= "/webapp" reloadable= "true" >
- <resourcelink global= "Jdbc/mysql" name=< Span class= "Attribute-value" > "Jdbc/mysql" type= " Javax.sql.DataSource " />
- </context>
Pros: reusability, controllability
Cons: Configuring a relative third method is a bit cumbersome, and each project has to be
Third, configure the global Jndi data source to apply to all apps deployed under Tomcat
Two steps, too.
The first step
Refer to the first step of the second type
The second step is to find the context.xml of Tomcat and add a ResourceLink node under the context node to reference the data source configured in the first step
The root node of this XML configuration file is <Context>
XML code
- <Context>
- <resourcelink global="Jdbc/mysql" name="Jdbc/mysql" type=" Javax.sql.DataSource " />
- <watchedresource>web-inf/web.xml</watchedresource>
- <Context>
Pros: reusability, disposable
Cons: No controllability
Spring reference to a Jndi data source
Add a bean to the applicationcontext.xml to replace the original datasource
XML code
- <jee:jndi-lookup id="DataSource" jndi-name="Jdbc/mysql" />
C3P0 configuration of the data source
The values of type and factory vary
Username=>user
Url=>jdbcurl
Driverclassname=>driverclass
XML code
- <Resource name="jdbc/mysql_c3p0" scope="shareable "
- type="Com.mchange.v2.c3p0.ComboPooledDataSource"
- factory="Org.apache.naming.factory.BeanFactory"
- jdbcurl= "jdbc:mysql://localhost:3306/test" driverclass="Com.mysql.jdbc.Driver "
- user="root" password="root" />
For more detailed information, please refer to: Java Advanced network www.javady.com
Three ways to configure Jndi data sources in Tomcat