Defining DataSource


Traditional JDBC has the provision to register the driver classes before it is actually used. A driver that is accessed via a DataSource object does not register itself with the DriverManager . Rather, a DataSource object is retrieved though a lookup operation and then used to create a Connection object.


Being registered with a JNDI naming service gives a DataSource object two major advantages over the DriverManager . First, an application does not need to hardcode driver information, as it does with the DriverManager . A programmer can choose a logical name for the data source and register the logical name with a JNDI naming service.


There are two ways to define a datasource in the server.xml


1. Defining under the global resources for the tomcat. This way the same datasource is shared by the different application in the server. But in the ideal world this would not be the case as all applications have seperate databases.


2. Defining in the <context> tag which describes  an application in the tomcat. So the resources are bound with only that application. Other application with different contexts can not use the datasource. The code for the same is as follows:-


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

    <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"

        username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3309/test?autoReconnect=true"/>


</Context>


 

                    

Copyright © 2012 VisualBuilder. All rights reserved