开发者

Tomcat6 MySql JDBC Datasource configuration

开发者 https://www.devze.com 2022-12-24 15:56 出处:网络
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.

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.开发者_如何学编程


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).


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>


You can declare your data source as a JNDI object and retrieve a datasource via a JNDI lookup:

DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

as documented here and here.

That's as bare-bones as you can get, so from there on, performance is completely up to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号