数据源DataSource的配置

一、J2EE数据源配置的标准是,

1、首先需要在Server.xml中配置数据源:

<Context docBase="TestDataSource" path="/TestDataSource" reloadable="true"    source="org.eclipse.jst.j2ee.server:TestDataSource">

<Resource name="jdbc/TaskDB" auth="Container"
      type="javax.sql.DataSource"
      maxActive="100" maxIdle="30"    maxWait="10000"
      username="root" password="neddy"
     driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/task?autoReconnect=true"/>

</Context>

2、然后必须在web.xml中加入<resource-ref>元素<resource-ref>元素表示在Web应用中引用JNDI资源
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TaskDB</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

 

二、上面的过程为J2EE配置数据源的标准,比较麻烦并且比较单一(当然可能还有其他方法)

下面列举Tomcat配置数据源的方法:

1、如果是在Tomcat中配置数据源的话的,如果按照上面的方法配置数据源的话,只需要在Server.xml中配置数据源即可,无需再Web.xml中加入<resource-ref>元素<resource-ref>元素表示在Web应用中引用JNDI资源。

2、Tomcat应用服务器下的项目,数据源也可以在项目Application下META-INF/context.xml,路径创建context.xml文件,内配置数据源,如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/TaskDB" auth="Container"
      type="javax.sql.DataSource"
      maxActive="100" maxIdle="30"    maxWait="10000"
      username="root" password="neddy"
     driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/task?autoReconnect=true"/>
</Context>

 

三、英文文档链接和说明如下:

 

http://wiki.apache.org/tomcat/UsingDataSources

 

How do I use DataSources with Tomcat?

When developing J2EE web applications, the task of database connection management can be daunting. Best practice involves using a J2EE DataSource to provide connection pooling, but configuring DataSources in web application servers and connecting your application to them is often a cumbersome process and poorly documented.

The usual procedure requires the application developer to set up a DataSource in the web application server, specifying the driver class, JDBC URL (connect string), username, password, and various pooling options. Then, the developer must reference the DataSource in his application's web.xml configuration file, and then access it properly in his servlet or JSP. Particularly during development, setting all of this up is tedious and error-prone.

With Tomcat the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don't have to mess with configuring the DataSource separately in the Tomcat server.xml, or referencing it in your application's web.xml file.

你可能感兴趣的:(dataSource)