DatasourceTomcat4

Unofficial Content

Defining a JDBC Datasource in Jakarta Tomcat 4.x

Scope: GeneXus Java 8.0 or higher and Jakarta Tomcat 4x

First Step
This can also be done from Tomcat Administrative Console. The main difference will be that in this case, the JNDI will be available for the entire server (all web apps) instead of being available for a specific Context.

In order to do this just Start Tomcat and enter the following URL: http://localhost:8080/admin

  • Click the option "Data Sources" and choose the option "Create New Data Source"
 
  • Enter the needed information with your specific information as shown in the image below. This data source is defined as an example for SQL Server





First Step (The same editing system files - Recommended for advanced users only)
Edit the file TOMCAT_HOME\conf\server.xml.
Find your application context definition within the <Context> and </Context> tags.
Once you find it paste the following before the tag </Context>

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"/>
 <ResourceParams name="jdbc/TestDB"> 
   <parameter> 
     <name>factory</name> 
     <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 
   </parameter> 
   <parameter> 
     <name>maxActive</name> 
     <value>100</value> 
   </parameter> 
   <parameter> 
     <name>maxIdle</name> 
     <value>30</value> 
   </parameter> 
   <parameter> 
     <name>maxWait</name> 
     <value>10000</value> 
   </parameter> 
   <parameter> 
    <name>username</name> 
    <value>MyUsername</value> 
   </parameter> 
   <parameter> 
    <name>password</name> 
    <value>MyPassword</value> 
   </parameter> 
   <parameter> 
      <name>driverClassName</name> 
      <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> 
   </parameter> 
   <parameter> 
     <name>url</name> 
             <value>jdbc:microsoft:sqlserver://MyServerxp:1433;databaseName=MyDatabase</value>
   </parameter> 
 </ResourceParams>

Where jdbc/TestDB is the JNDI name of the JDBC Datasource.
In this case, it's a definition of a data source pointing to an SQL Server.
Fields in bold should be modified with your specific information.

Notes

  • With the declaration above, a SQL error might be generated if there is no SelectMethod information in the url value (for MS SqlServer). Try with:
<parameter> 
 <name>url</name>  
 <value>jdbc:microsoft:sqlserver://<server>:1433;databaseName=<database>;SelectMethod=Cursor</value>
</parameter>
  • Sometimes, there is no Context declaration for our web app in TOMCAT_HOME\conf\server.xml file, and the data source is declared as a global resource. In this case, we could create the context information for the web app with a link to the global data source inside. Something like this:
<Context docBase="<webapp>" path="/<webapp>" workDir="work/Catalina/localhost/<webapp>"> 
 <ResourceLink name="jdbc/TestDB" global="jdbc/TestDB" type="javax.sql.DataSource" />  
</Context>

This Context definition should be stated between the <host></host> tags.

Second Step
Edit the file TOMCAT_HOME\webapps\<YOUR_WEBAPP>\WEB-INF\web.xml.
Add the following piece of code before the </webapp> tag:

<resource-ref>
     <description>DB Connection</description> 
     <res-ref-name>jdbc/TestDB</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
</resource-ref>

Where jdbc/TestDB is the JNDI name of the JDBC Datasource.

Third Step
Copy the JDBC Driver libraries into the TOMCAT_HOME\common\lib.

Fourth Step
Edit the application DBMS Options and set the following properties:

Use datasource for web based applications = True
JDBC datasource = java:comp/env/jdbc/TestDB

Where jdbc/TestDB is the JNDI name of the JDBC Datasource.