Tomcat和JDBC连接池 [英] Tomcat and JDBC connection pooling

查看:165
本文介绍了Tomcat和JDBC连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tomcat建立与mysql databe的连接池.我的简单应用名为Projekt,在我拥有的Apache/conf/Catalina/localhost中的Projekt.xml中

I am trying to set up connection pooling to mysql databe with tomcat. My simple app is called Projekt, in my Projekt.xml in Apache/conf/Catalina/localhost I have

<Context docBase="Projekt.war" path="/Projekt">
  <Resource name="jdbc/mysqldb"
      auth="Container"
  factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      type="javax.sql.DataSource"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/Music"
      username="andrzej"
      password="qazxsw"
      maxActive="20"
      maxIdle="30"
      maxWait="5"
  />
</Context> 

我的应用的web.xml

web.xml of my app

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>org.jtp.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/Hai</url-pattern>
</servlet-mapping>

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

并且在我的Apache/lib文件夹中

and in my Apache/lib folder I have

mysql-connector-java-5.1.18-bin.jar

但是当我执行这段代码时:

but when I execute this code:

Context initContext  = new InitialContext();
dataSource = (DataSource)initContext.lookup("java:comp/env/jdbc/mysqldb");
System.out.println(dataSource.getConnection().createStatement().
            execute("select * from Users"));

我遇到异常

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

我现在很困惑,在某些地方,我读到它可能是由于没有将驱动程序放置在tomcat/lib中引起的,但是我拥有了它并且可以正常工作,因为当我使用手动连接测试驱动程序时,它可以正常工作.

I am puzzled now, in some places I read that it may be caused by not placing driver in tomcat/lib, but I have it and it works, because when I tested the driver with manual connections it worked.

对于我的设置,我尝试遵循 http://people.apache.org/~fhanik/jdbc-pool /jdbc-pool.html

For my setup I was trying to follow http://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html

终于奏效了,似乎我在其中一个文件中有一些左上下文标记,因此在解析时他覆盖了其他属性,所以最后都是我的错.

Finally got it working, it seemed that I had some left context tags in one of the files so when parsing he overriden other attributes, so it is all my fault at the end.

推荐答案

好像您丢失了Context envCtx = (Context) initCtx.lookup("java:comp/env");
JNDI查找应按以下方式进行:

Looks like you are missing Context envCtx = (Context) initCtx.lookup("java:comp/env");
JNDI lookup should be done like this:

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");

// Allocate and use a connection from the pool
Connection conn = ds.getConnection(); 

来自 http://tomcat.apache.org/的

文档tomcat-7.0-doc/jndi-resources-howto.html .

这篇关于Tomcat和JDBC连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆