[opencms-dev] database access via java class and jsp

Michael Powell mwpowell at adelphia.net
Thu Mar 24 01:22:51 CET 2005


For the benefit  of others here I found the solution on one of sun's developer forums. The answer is as follows:

I added the following tags to the web.xml file which describe my datasource:

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

Please note that these tags must come in a certain order as described in the DTD.....
<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, servlet*, servlet-mapping*, session-config?,
mime-mapping*, welcome-file-list?, error-page*, taglib*,
resource-ref*, security-constraint*, login-config?, security-role*,
env-entry*, ejb-ref*)>


Secondly, I added a context tag set with the appropriate parameters as described in the tomcat HOW-TO documents. My addition in server.xml was....
<Context path="/opencms" docBase="opencms"
        debug="5" reloadable="true" crossContext="true">

  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_opencms_log." suffix=".txt"
             timestamp="true"/>

  <Resource name="jdbc/javatest"
               auth="Container"
               type="javax.sql.DataSource"/>

  <ResourceParams name="jdbc/javatest">
    <parameter>
      <name>factory</name>
      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>

    <!-- Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->
    <parameter>
      <name>maxActive</name>
      <value>100</value>
    </parameter>

    <!-- Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         -->
    <parameter>
      <name>maxIdle</name>
      <value>30</value>
    </parameter>

    <!-- Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->
    <parameter>
      <name>maxWait</name>
      <value>10000</value>
    </parameter>

    <!-- MySQL dB username and password for dB connections  -->
    <parameter>
     <name>username</name>
     <value>root</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value></value>
    </parameter>

    <!-- Class name for mm.mysql JDBC driver -->
    <parameter>
       <name>driverClassName</name>
       <value>org.gjt.mm.mysql.Driver</value>
	<!--<value>com.mysql.jdbc.Driver</value>-->
    </parameter>

    <!-- The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->
    <parameter>
      <name>url</name>
      <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
    </parameter>
  </ResourceParams>
</Context>

You will of couse have to change the values here as appropriate for your own application in order for this to work.

Mike
---- Michael Powell <mwpowell at adelphia.net> wrote: 
> All,
> 
> I have developed a jsp app and java class which queries a database using Tomcat and MySql. I would now like to deploy this app within the framework of OpenCms. I have copied my jsp files into OpenCms and my java class to the WEB-INF/classes/ subdirectory. However, my app cannot connect to the database when within the OpenCms framework.
> 
> How do I properly configure OpenCms to access the MySql database? Changes to the web.xml for opencms or perhaps changes to the Tomcat server.xml file?
> 
> Thanks.
> 
> Mike
> mwpowell at adelphia dot net
> 
> 
> _______________________________________________
> This mail is send to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://mail.opencms.org/mailman/listinfo/opencms-dev




More information about the opencms-dev mailing list