Women in Technology

Hear us Roar



Article:
  Deploying Web Applications to Tomcat
Subject:   Problems connecting to my Database & fairly confused
Date:   2002-05-04 15:34:30
From:   thehitman@mindspring.com
I want to access the Sybase ASA database in two ways:
1) getServlet().findDataSource(null)
2) JNDI reference JDBC/BPPetstore


struts-config.xml


- <data-sources>


<!-- What's the key value here of Action.DATA_SOURCE_KEY. ? -->
- <data-source>
<set-property property="autoCommit" value="true" />
<set-property property="description" value="Example Data Source Configuration" />
<set-property property="driverClass" value="com.sybase.jdbc2.jdbc.SybDriver" />
<set-property property="maxCount" value="4" />
<set-property property="minCount" value="2" />
<set-property property="password" value="sql" />
<set-property property="url" value="jdbc:sybase:Tds:BPPetstore:2638/BPPetstore" />
<set-property property="user" value="dba" />
</data-source>
</data-sources>


web.xml


<resource-ref>
<res-ref-name>What name should be here ?</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>




<!ELEMENT data-sources (data-source*)>
<!ATTLIST data-sources id ID #IMPLIED>



<!-- The "data-source" element describes a JDBC 2.0 Standard Extension data
source object (that implements javax.sql.DataSource) which will be
configured according to the properties and nested elements found here,
and made available as a servlet context attribute (i.e. application
scope bean). The following attributes are required:


key Servlet context attribute key under which this data
source will be stored. Default is the value specified
by string constant Action.DATA_SOURCE_KEY.


type Fully qualified Java class name of the implementation
class (must implement javax.sql.DataSource). Default
value is 'org.apache.struts.util.GenericDataSource'.


NOTE: The following attributes are defined by the default data source
implementation, and only take effect for that class or subclasses of
that class.


WARNING: The use of these attributes is deprecated. You should use
nested <set-property> elements to configure *all* properties of
your data source implementation.


autoCommit The default auto-commit state for newly created
connections.


description The description of this data source.


driverClass The Java class name of the JDBC driver to be used.
[REQUIRED]


loginTimeout The maximum number of seconds to wait for a connection
to be created or returned. Default is driver dependent.


maxCount The maximum number of connections to be created.


minCount The minimum number of connections to be created.


password The database password to use when connecting. [REQUIRED]


readOnly The default read-only state for newly created
connections.


url The JDBC URL to use when connecting. [REQUIRED]


user The database username to use when connecting. [REQUIRED]


-->
<!ELEMENT data-source (set-property*)>
<!ATTLIST data-source id ID #IMPLIED>
<!ATTLIST data-source key %BeanName; #IMPLIED>
<!ATTLIST data-source type %ClassName; #IMPLIED>
<!-- All of the following attributes are deprecated. Use a nested -->
<!-- set-property element to configure data source properties. -->
<!ATTLIST data-source autoCommit %Boolean; #IMPLIED>
<!ATTLIST data-source description CDATA #IMPLIED>
<!ATTLIST data-source driverClass %ClassName; #IMPLIED>
<!ATTLIST data-source loginTimeout %Integer; #IMPLIED>
<!ATTLIST data-source maxCount %Integer; #IMPLIED>
<!ATTLIST data-source minCount %Integer; #IMPLIED>
<!ATTLIST data-source password CDATA #IMPLIED>
<!ATTLIST data-source readOnly %Boolean; #IMPLIED>
<!ATTLIST data-source url CDATA #IMPLIED>
<!ATTLIST data-source user CDATA #IMPLIED>




<!ELEMENT resource-env-ref-type (#PCDATA)>


<!--
The resource-ref element contains a declaration of a servlet's
reference to an external resource. It consists of an optional
description, the resource manager connection factory reference name,
the indication of the resource manager connection factory type
expected by servlet code, the type of authentication
(Application or Container), and an optional specification of the
shareability of connections obtained from the resource (Shareable or
Unshareable).


%USED


Example:


<resource-ref>
<res-ref-name>jdbc/EmployeeAppDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>


-->


<!ELEMENT resource-ref (description?, res-ref-name, res-type, res-auth, res-sharing-scope?)>


<!--
The res-ref-name element specifies the name of the resource
factory reference name.
-->


<!ELEMENT res-ref-name (#PCDATA)>


<!--
The res-ref-name element specifies the name of a resource manager
connection factory reference. The name is a JNDI name relative to the
java:comp/env context. The name must be unique within web application.
-->


<!ELEMENT res-type (#PCDATA)>


<!--
The res-auth element specifies whether the servlet code signs
on programmatically to the resource manager, or whether the Container
will sign on to the resource manager on behalf of the servlet. In the
latter case, the Container uses information that is supplied by the
Deployer.


The value of this element must be one of the two following:


<res-auth>Application</res-auth>
<res-auth>Container</res-auth>
-->


<!ELEMENT res-auth (#PCDATA)>


<!--
The res-sharing-scope element specifies whether connections obtained
through the given resource manager connection factory reference can be
shared. The value of this element, if specified, must be one of the
two following:


<res-sharing-scope>Shareable</res-sharing-scope>
<res-sharing-scope>Unshareable</res-sharing-scope>


The default value is Shareable.
-->


<!ELEMENT res-sharing-scope (#PCDATA)>


<!--


Using jConnect as a JNDI (Pooled) jdbc datasource under TomCat 4.0.3
By Bob Peck (Sybase Technical Support)

Note: You do NOT need to register a custom Object Factory. "com.sybase.jdbc2.jdbc.SybObjectFactory" must not appear anywhere in the configution.

1) In your $CATALINA/conf/server.xml file, define a resource similiar to the following (I defined my resource within a <Context> </Context>
bracket):

------------------------------------------------------------------------

<Resource name="jdbc/datasourcename" auth="Container"
type="javax.sql.ConnectionPoolDataSource"/>
<ResourceParams name="jdbc/datasourcename">

<parameter> <name>user</name> <value>your_userid</value> </parameter>
<parameter> <name>password</name><value>your_password</value></parameter>

<parameter>
<name>driverClassName</name><value>com.sybase.jdbc2.jdbc.SybDriver</value>
</parameter>

<parameter>
<name>driverName</name>
<value>jdbc:sybase:Tds:dataserverhostname:portnum/your_dbname</value>
</parameter>

<parameter><name>initialPoolSize</name><value>2</value></parameter>

</ResourceParams>

-------------------------------------------------------------------------------


2) In your webapp deployment descriptor,
(ie, in $CATALINA/webapps/webappname/WEB-INF/web.xml)
identify that server resource as follows:


<resource-ref>
<res-ref-name> jdbc/datasourcename </res-ref-name>
<res-type>javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>


NOTICE:

In the server.xml: type="javax.sql.ConnectionPoolDataSource",
yet in the web.xml: <res-type> javax.sql.DataSource </res-type>



3) Copy jConnect classes jconn2.jar into your
%CATALINA%/common/lib subdirectory.


4) In your jsp,
code similiar to the following will work: