0

我正在尝试使用 Tomcat 7 中的 MySql 连接池设置领域。我已阅读所有文档并阅读了人们遇到的许多类似问题,但都没有运气。

我已经为连接池配置了 Tomcat 7,我使用 test.jsp 成功测试了该连接池(图 1)。

图 2 是我的应用程序上下文文件。请注意,注释掉的领域有效(显然在没有注释掉时),但我相信它不使用连接池或 jndi 资源。相反,它每次都会创建一个新连接。未注释掉的领域,引用了 JNDI 配置的连接池。这与图 1 中的测试代码使用的 JNDI 资源相同。但是,当领域尝试验证用户登录请求时,它会引发异常(图 3),说明Name jdbc is not bound in this Context. 我在这里使用 DataSourceRealm 是否弄错了?我错过了什么?

图 1 - test.jsp(工作正常):

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/TestDB">
select email, password from users
</sql:query>

<html>
<head>
    <title>DB Test</title>
</head>
<body>

    <h2>Results</h2>

    <c:forEach var="row" items="${rs.rows}">
        Email ${row.email}<br/>
        Password ${row.password}<br/>
    </c:forEach>

</body>
</html>

图 2 - Context.xml:

<context>

  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" initialSize="10"
    jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState; 
    org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
    jmxEnabled="true" logAbandoned="true" maxActive="100" maxWait="10000"
    minEvictableIdleTimeMillis="30000" minIdle="10" name="jdbc/TestDB"
    password="security" removeAbandoned="true" removeAbandonedTimeout="60"
    testOnBorrow="true" testOnReturn="false" testWhileIdle="true"
    timeBetweenEvictionRunsMillis="30000" type="javax.sql.DataSource"
    url="jdbc:mysql://localhost:3306/estimate" username="security"
    validationInterval="30000" validationQuery="SELECT 1" />

  <!-- <Realm className="org.apache.catalina.realm.JDBCRealm" -->
  <!-- driverName="com.mysql.jdbc.Driver" -->
  <!-- connectionURL="jdbc:mysql://localhost:3306/estimate?user=security&amp;password=security" -->
  <!-- userTable="users" userNameCol="email" userCredCol="password" -->
  <!-- userRoleTable="user_roles" roleNameCol="role" /> -->

  <Realm className="org.apache.catalina.realm.DataSourceRealm"
    dataSourceName="jdbc/TestDB" userTable="users" userNameCol="email"
    userCredCol="password" userRoleTable="user_roles" roleNameCol="role" />

</context>

图 3 - 我得到的异常:

Jul 05, 2013 2:16:03 PM org.apache.catalina.realm.DataSourceRealm open
SEVERE: Exception performing authentication
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:803)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.catalina.realm.DataSourceRealm.open(DataSourceRealm.java:394)
    at org.apache.catalina.realm.DataSourceRealm.authenticate(DataSourceRealm.java:285)
    at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:282)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:440)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

这可能是我犯的一些愚蠢的错误,但我已经为此工作了好几天。

4

1 回答 1

1

遇到了同样的问题,通过将localDataSource="true"添加到领域来解决它。

<Realm className="org.apache.catalina.realm.DataSourceRealm"
    localDataSource="true"
    dataSourceName="jdbc/Tomcat"
    userTable="users" userNameCol="user_name" userCredCol="user_pass"
    userRoleTable="user_roles" roleNameCol="role_name"/>
于 2014-01-21T09:01:32.240 回答