I describe here my favorite solution to define the datasource of my web applications.
To do that in Tomcat :
- Copy the JDBC driver jar into the Tomcat lib directory
- define the datasource in the file server.xml:
<GlobalNamingResources> <Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/myGlobalDatasource" password="password" type="javax.sql.DataSource" url="jdbc:oracle:thin:@[server]:[port]:[sid]" username="user"/> </GlobalNamingResources>
- map this datasource to your application using its context.xml file:
<ResourceLink global="jdbc/globalDatasource" name="jdbc/applicationDatasource" type="javax.sql.DataSource"/>
- And now, when you want to use it (using Spring):
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/applicationDatasource" /> <property name="resourceRef" value="true" /> </bean>
Done!
This example uses an Oracle connection but it can be easily adapted for another database.