Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Boell Fischer   on Aug 26 In Java Category.

  
Question Answered By: Kerry Wright   on Aug 26

You must be carefull where your Context tag is. This server.xml works for me. Context tag is inside Host tag.


<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN" debug="0">


<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"
debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"
debug="0"/>

<!-- Global JNDI resources -->
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved">
</Resource>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>

</GlobalNamingResources>

<Service name="Catalina">
<Connector
port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
<Connector port="8009"
enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />

<Engine name="Catalina" defaultHost="localhost" debug="0">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
debug="0" resourceName="UserDatabase"/>
<Host name="localhost" debug="0" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="localhost_log." suffix=".txt"
timestamp="true"/>

<!-- Define properties for each web application. This is only needed
if you want to set non-default properties, or have web application
document roots in places other than the virtual host's appBase
directory. -->

<!-- tomcat  Root Context -->
<!--
<Context path="" docBase="ROOT" debug="0">
-->

<Context path="/SISSECWeb" docBase="SISSECWeb"
debug="5" reloadable="true" crossContext="true">

<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/SISSECDS" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/SISSECDS">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>SISSEC</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:SISSEC/SISSEC@...:1521:ORALINUX</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>SISSEC</value>
</parameter>
</ResourceParams>
</Context>
</Host>

</Engine>

</Service>

</Server>

///////////////////////////////////////////////////////

Configuration of "server.xml"

Assumptions:
1) I assume that your application is named "myapp"
2) I assume that it resides under "{TOMCAT_HOME}/webapps/myapp/"
3) I assume that you want to connect to DBMS that is in your computer (localhost)
4) I assume that the name of the database you want to connect is "MyappDB"
5) I assume that you use mysql  JDBC driver

So put the following entry in "server.xml" under the tag <Engine>:

<Server>
<Service>
<Engine>
...

<Context docBase="mayapp" path="/myapp" workDir="work/Catalina/localhost/myapp">

<Resource auth="Container" description="MyApp DataSource Reference" name="jdbc/MyappDB" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" password="[i]your_password[/i]" maxIdle="2" maxWait="5000" username="[i]your_username[/i]" url="jdbc:mysql://localhost/MyappDB" maxActive="4"/>
</Context>

...
</Engine>
</Service>
</Server>
Configuration of "web.xml"

To have well configurred "web.xml" file for your web app you should have the following entry under <web-app> tag:

<web-app>
...

<resource-ref>
<description>MyApp DataSource Reference</description>
<res-ref-name>jdbc/MyappDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

...
</web-app>

Besids manual configuration you can use Tomcat's admin tool.

1) Login to the admin tool (http://localhost:8080/admin/)
2) Using the menu like browser expand the tree as follows:
"Tomcat Server->Service (Catelina)->Host (localhost)->Context (/myapp)-> Resources->"
3) Click on "Data Sources" and you should be able to the list of see your data sources
4) From Data Source Actions drop down lis (uper right in the eindow) select "Create New Data Source"

5) Fill the form as follows:
5.1) JNDI Name = jdbc/MyappDB
5.2) Data Source URL = jdbc:mysql://localhost/school
5.3) JDBC Driver Class = com.mysql.jdbc.Driver
5.4) User Name = your_username
5.5) Password = your_password
5.6) Max. Active Connections = 4
5.7) Max. Idle Connections = 2
5.8) Max. Wait for Connection = 5000
5.9) Validation Query =

6) Press the "Save" button (upper right)
7) Press "Commit Changes" button
8) Restart your Tomcat server

Share: