Lets start with "How to configure a datasource in Apache Tomcat".
There is another way of configuring datasource in Tomcat, but I prefer this solution as the configuration resides in the application war file. Simply copy your war file in a tomcat server along with the required jdbc jar and your application is up and running. I wish I can bundle the jdbc jar also in war but unfortunately it doesn't work.
Create a file name "context.xml" (any other name will not work) in META-INF directory of your war file. The content of the context.xml will look like this -
<Context path="/travel" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/travel" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="user" password="password" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/travel?autoReconnect=true"/>
</Context>
Add this in the web.xml of your war -
<resource-ref>
<description>Datasource</description>
<res-ref-name>jdbc/travel</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
The res-ref-name should match the //Resource@name defined in the context.xml
Copy the required jdbc driver jar in $TOMCAT_HOME/lib directory. In this case its mysql-connector-java-5.1.6.jar for mysql database.
No comments:
Post a Comment