之前我已经设置了JNDI资源,但是我遇到了一个问题,我不确定如何在我的apache-tomcat-8.0.36
服务器上进行更正。
我的context.xml
文件包含以下内容:
<ResourceLink name="jdbc/FsEDBAdmin" global="jdbc/FsEDBAdmin" type="javax.sql.DataSource" />
<ResourceLink name="jdbc/FsEDBUser" global="jdbc/FsEDBUser" type="javax.sql.DataSource" />
<Resource name="jdbc/FsEDBAdmin" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://location"
username="user_admin" password="pass"
maxActive="20" maxIdle="10" maxWait="-1"/>
<Resource name="jdbc/FsEDBUser" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://location"
username="user_user" password="pass"
maxActive="20" maxIdle="10" maxWait="-1"/>
我的web.xml
:
<resource-ref>
<description>Admin Connection</description>
<res-ref-name>jdbc/FsEDBAdmin</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>User Connection</description>
<res-ref-name>jdbc/FsEDBUser</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
我也在使用这些连接来定义Realm
进行身份验证:
server.xml
:
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Realm className="org.apache.catalina.realm.DataSourceRealm"
digest="digest_method" dataSourceName="jdbc/FsEDBAdmin" userTable="schema.table_name"
userNameCol="name_col" userCredCol="pass_col"
userRoleTable="schema.table_name" roleNameCol="rolename_col"
localDataSource="true"/>
</Realm>
但是当我启动我的应用程序时,我收到了标题中提到的错误。如果需要,我可以提供完整的堆栈跟踪。
我想补充一点,这是在一点上工作,最近的变化是使用Realm
进行身份验证。显然我在一个地方或另一个地方定义这些资源时犯了一个错误,所以另一组眼睛告诉我哪里会受到高度赞赏。谢谢。
编辑:这就是我调用资源的方式:
import path.to.CommonTools;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
/**
*
* @author Michael Potts
*/
public enum Resource {
INSTANCE;
private static BasicDataSource basicAdmin = null;
private static BasicDataSource basicUser = null;
private static final String JNDI_PREFIX = "java:/comp/env";
private static final String ADMIN_DB_NAME = "jdbc/FsEDBAdmin";
private static final String USER_DB_NAME = "jdbc/FsEDBUser";
/**
* Secure method that gives a connection from the pool for an Admin user
* @return java.sql.Connection
* @throws Exception Throws if Context isn't properly defined on Server
*/
public static Connection getAdminConnection() throws Exception {
try {
if(basicAdmin == null) { //1st time load
Context dbContext = (Context) new InitialContext().lookup(JNDI_PREFIX);
basicAdmin = (BasicDataSource) dbContext.lookup(ADMIN_DB_NAME);
}
return basicAdmin.getConnection();
} catch (NamingException | SQLException e) {
throw new RuntimeException("\nInvalid JNDI resource: " + ADMIN_DB_NAME + CommonTools.getStackTrace(e));
}
}
/**
* Secure method that gives a connection from the pool for a standard user
* @return java.sql.Connection
* @throws Exception Throws if Context isn't properly defined on Server
*/
public static Connection getUserConnection() throws Exception {
try {
if(basicUser == null) { //1st time load
Context dbContext = (Context) new InitialContext().lookup(JNDI_PREFIX);
basicUser = (BasicDataSource) dbContext.lookup(USER_DB_NAME);
}
return basicUser.getConnection();
} catch (NamingException | SQLException e) {
throw new RuntimeException("\nInvalid JNDI resource: " + USER_DB_NAME + CommonTools.getStackTrace(e));
}
}
}
答案 0 :(得分:2)
因此我的解决方案是删除除Resource
中的基本context.xml
定义之外的所有条目。这包括删除ResourceLink
个条目。因此,如果您已尝试过所有内容并且您正在收到此错误,就像我只是忽略所有文档而只是在Context.xml中定义。