Monday 27 April 2009

2-SIMPLE AND COMPLETE DATA SOURCE EXAMPLE

After you make sure that you can run a Servlet, you create the real servlet which does the DB access.
But in order to access the MySQL DB I had to create the datasource definition in the context.xml file.
The context.xml file may be placed in the WEBCONTENT file(whatever its name) at the same level with WEB-INF.

FILE: context.xml

<!-- The contents of this file will be loaded for each web application -->
<Context >
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="3391309"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testars?autoReconnect=true"
maxActive="8"
maxIdle="4" />
</Context>

After my program worked I also tried the below context.xml with pooling options. (It looked as if
it worked OK. I did not have the means to measure it and also my data was very small).

<!-- The contents of this file will be loaded for each web application -->
<Context >
<Resource name="jdbc/MySQLDB"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="3391309"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testars?autoReconnect=true"
maxActive="8"
maxIdle="4" />
<ResourceParams name="jdbc/MySQLDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
</Context>

Apache TOMCAT’s “JNDI Datasource HOW-TO”
http://tomcat.apache.org/tomcat-4.1-doc/jndi-datasource-examples-howto.html gives more information on this matter, using the server.xml file of TOMCAT and using
The related JARs etc.

( prev ) ( next )