简体   繁体   English

如何在 Tomcat 7 中使用 MySql 连接池设置领域?

[英]How do I set up a Realm with a MySql Connection Pool in Tomcat 7?

I am trying to set up a Realm using a MySql Connection Pool in Tomcat 7. I have read all the documentation and read through many similar problems people have had, all with no luck.我正在尝试使用 Tomcat 7 中的 MySql 连接池来设置一个领域。我已经阅读了所有文档并阅读了人们遇到的许多类似问题,但都没有运气。

I have cofigured Tomcat 7 for a connection pool which I successfully tested using a test.jsp (Figure 1).我已经为连接池配置了 Tomcat 7,我使用 test.jsp 成功测试了该连接池(图 1)。

Figure 2 is my context file for the application.图 2 是我的应用程序上下文文件。 Note that the commented out realm works (obviously when not commented out), but I believe that does not use a connection pool or jndi resource.请注意,注释掉的领域有效(显然当未注释掉时),但我相信它不使用连接池或 jndi 资源。 Instead it creates a new connection each time.相反,它每次都会创建一个新连接。 The realm that is not commented out, references the JNDI configured connection pool.未注释的领域,引用 JNDI 配置的连接池。 This is the same JNDI resource the test code in figure 1 uses.这与图 1 中的测试代码使用的 JNDI 资源相同。 However, when the realm attempts to authenticate a user login request, it throws an exception (figure 3) saying the Name jdbc is not bound in this Context .但是,当领域尝试对用户登录请求进行身份验证时,它会引发异常(图 3),说明Name jdbc is not bound in this Context Am I mistaken in my use of a DataSourceRealm here?我在这里使用 DataSourceRealm 是错误的吗? What am I missing?我错过了什么?

Figure 1 - test.jsp (works fine):图 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>

Figure 2 - Context.xml:图 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>

Figure 3 - the exception I get:图 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)

It'll probably be some dumb mistake I'm making but been working on it for days now.这可能是我犯的一些愚蠢的错误,但现在已经研究了好几天了。

Met the same problem with you, solved it by adding localDataSource="true" to the realm.遇到了与您相同的问题,通过向领域添加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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM