2013-07-05 87 views
0

我正嘗試在Tomcat 7中使用MySql連接池設置Realm。我已閱讀所有文檔,並通讀了許多人遇到的類似問題,所有這些都沒有運氣。如何使用Tomcat 7中的MySql連接池設置Realm?

我已經配置了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) 

這可能會造成一些愚蠢的錯誤,但現在已經有好幾天了。

+1

http://stackoverflow.com/questions/13730192/tomcat-jdbc-vs-datasource-realm?看起來你所缺少的和OP一樣 - 'Realm'中的'localDataSource =「true」'' –

+0

謝謝!我花了幾個小時瀏覽論壇,文件和搜索,但從未看過那​​個。非常棒!作爲回答發佈,我會標記它。 – bzuillsmith

+0

呃,我所做的只是一個很好的谷歌; upvote那裏的答案 - 他們是值得的。 –

回答

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"/>