2013年7月30日星期二

Configuring Tomcat data source

1, Method 1: Configure server.xml

1) tomcat conf directory under the installation path of server.xml, in and add the following content between the tags :

<Resource name="jdbc/appDS" auth="Container" 
type
="javax.sql.DataSource"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true"
username
="root"
password
="root"
maxActive
="100"
maxIdle
="20"
maxWait
="10000"/>

2) tomcat conf directory under the installation path of context.xml, in and add the following content between the tags :

<ResourceLink name="jdbc/appDS" global="jdbc/appDS"  type="javax.sql.DataSource"/>

3) web.xml configuration

<resource-ref> 
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

2, way: configure the context.xml

1) tomcat conf directory under the installation path of context.xml, in and add the following content between the tags :

<Resource name="jdbc/appDS" auth="Container" 
type
="javax.sql.DataSource"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true"
username
="root"
password
="root"
maxActive
="100"
maxIdle
="20"
maxWait
="10000"/>

2) web.xml configuration

<resource-ref> 
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3, Three ways: in the project / WebRoot / META-INF / directory, create a context.xml file , as follows :

<?xml version='1.0' encoding='utf-8'?> 
<Context>
<Resource name="jdbc/appDS" auth="Container"
type
="javax.sql.DataSource"
driverClassName
="com.mysql.jdbc.Driver"
url
="jdbc:mysql://localhost:3306/app?rewriteBatchedStatements=true"
username
="root"
password
="root"
maxActive
="100"
maxIdle
="20"
maxWait
="10000"/>
</Context>

web.xml configuration is as follows :

 <resource-ref> 
<description>app_DataSource</description>
<res-ref-name>jdbc/appDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

4, how to use

1) used in the program

Context initContext = new InitialContext(); 
Context ctx
= (Context)initContext.lookup("java:/comp/env");
DataSource ds
= (DataSource)ctx.lookup("jdbc/appDS");
Connection conn = ds.getConnection();
//????
//...

//????

2) in the spring configuration file using the

 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
<property name="jndiName"><value>java:comp/env/jdbc/appDS</value></property>
</bean>

1 条评论:

  1. I've got the exercise on sun networking tutorial to work so now need to spend more time on how it works.

    回复删除