Tomcat6 MySql JDBC Datasource configuration

10,033

Solution 1

Believe it or not, people were writing applications before Spring and some are still not using it :) In your case, you could use Tomcat connection pool (and there is a complete configuration example for MySQL in the documentation). Let me summarize it:

First, put your driver in $CATALINA_HOME/lib.

Then, configure a JNDI DataSource in Tomcat by adding a declaration for your resource to your Context:

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

    <!-- maxActive: 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 -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: 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.
         -->

    <!-- username and password: MySQL dB username and password for dB connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->

    <!-- url: The JDBC connection url for connecting to your MySQL dB.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>

</Context>

Declare this resource in your web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <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>
</web-app>

And get the datasource with a JNDI lookup in your application:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

Note that such lookup is usually coded in a ServiceLocator (when you can't have a a DI container or a framework inject it for you).

Solution 2

I used to get error with sybase, I had missing META-INF folder in WebContent folder. Putting context.xml in that fixed the error Cannot create JDBC driver of class '' for connect URL 'null'... // www.abbulkmailer.com My context.xml looks like

<Context path="/reports" docBase="reports" debug="5" reloadable="true" crossContext="true">
<Resource name='jdbc/ASCSybaseConnection'
      auth='Container'
      type='javax.sql.DataSource'
      username='fdd'
      password='555'
      driverClassName='com.sybase.jdbc2.jdbc.SybDriver'
      maxActive='100'
      maxIdle='100'
      minIdle='10'
      removeAbandoned="true"
      removeAbandonedTimeout="60"
      testOnBorrow="true"
      logAbandoned="true"
      url='jdbc:sybase:Tds:1.3.4.5:654/DB'/> 
</Context>
Share:
10,033
Bob
Author by

Bob

Updated on June 24, 2022

Comments

  • Bob
    Bob almost 2 years

    I've always used Spring's dependency injection to get datasource objects and use them in my DAOs, but now, I have to write an app without that.

    With Spring I can write something like this:

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1/app?characterEncoding=UTF-8" />
        <property name="username" value="u" />
        <property name="password" value="p" />
    </bean>
    

    But how can I use datasource in my DAOs without Spring or anything? I'm using servlets and JSPs only. Performance is very important factor.

  • BalusC
    BalusC almost 14 years
    It's beyond me why this didn't have at least a single upvote.
  • Devanshu Mevada
    Devanshu Mevada almost 14 years
    @BalusC: Damn, I'll never get that Unsung Hero badge! Just kidding of course, thank you very much.
  • BalusC
    BalusC almost 14 years
    Lol. You've got to have a long journey.
  • Devanshu Mevada
    Devanshu Mevada almost 14 years
    @BalusC If that ever happens, I quit!
  • itsraja
    itsraja over 10 years
    @PascalThivent which location is preferable for the Context file