Tomcat 8.0.15 Oracle 11数据库和jdbc javax.naming.NamingException

时间:2014-12-22 15:59:59

标签: java oracle tomcat jdbc junit4

我有一个用于建立报告的应用程序,现在我需要将此应用程序移动到Web环境。

为此,我使用的是Tomcat 8.0.15和Oracle Database 11g企业版。

在我的TOMCAT_HOME \ conf \ server.xml中,我有以下代码:

<Resource auth="Container" 
    driverClassName="oracle.jdbc.OracleDriver" 
    maxIdle="10" 
    maxTotal="20" 
    maxWaitMillis="-1" 
    name="jdbc/reportDataSource" 
    username="some_username"
    password="some_pass" 
    type="javax.sql.DataSource" 
    url="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = some.host)(PORT = some.port)))(CONNECT_DATA =(SID = SOME_SID)(SERVICE_NAME = SOME_SERVICE)))"/>

因此,在我的PROJECT_HOME \ WebContent \ WEB-INF \ web.xml中,我有以下内容:

<resource-ref>
    <description>Oracle Datasource definition</description>
    <res-ref-name>jdbc/reportDataSource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

最后在我的代码中,我有一个Java类,其中包含以下内容:

private void init() throws NamingException, SQLException {

    try {

        InitialContext initialContext = new InitialContext(); // JNDI initial context
        Context eventContext = (Context) initialContext.lookup("java:comp/env/jdbc/reportDataSource"); // Event context
        dataSource = (DataSource) eventContext.lookup("jdbc/reportDataSource"); // JNDI lookup          
        databaseConnection = dataSource.getConnection(); // database connection through data source

    } catch (SQLException se) {
        throw new SQLException("Connection object was not created. Rejected by host or not found.");
    } catch (NamingException ne) {
        throw new NamingException(ne.getMessage());
    }
}

最后在我的项目根目录中,我有以下测试设置:

@Before
public void setUp() throws Exception {

    dbConnectorManager = new DatabaseConnectorManager();
    assertNotNull(dbConnectorManager);
}

当我调用DatabaseConnectorManager()时,它调用此问题中显示的init()方法。但是,当我执行我的测试时,我得到以下与行相关的错误:

entContext eventContext = (Context) initialContext.lookup("java:comp/env/jdbc/reportDataSource"); // Event context

因此,由于以下错误,无法设置JNDI:

javax.naming.NamingException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

请告诉我是否可以创建这些JUnit测试以测试连接,或者我是否只能通过servlet进行测试?

我的配置有什么问题吗?

更新

我进行了改编,但现在我收到以下错误:

javax.naming.NamingException: Name [jdbc/reportDataSource] is not bound in this Context. Unable to find [jdbc].

我的数据源现在如下所示:

InitialContext initialContext = new InitialContext(); // JNDI initial context
        Context eventContext = (Context) initialContext.lookup("java:comp/env"); // Event context
        dataSource = (DataSource) eventContext.lookup("jdbc/reportDataSource"); // JNDI lookup          
        databaseConnection = dataSource.getConnection(); // database connection through data source

2 个答案:

答案 0 :(得分:0)

&#39;不确定这是 错误,但您可能在此处输入错字:

Context eventContext =
    (Context) initialContext.lookup("java:comp/env/jdbc/reportDataSource"); // Event context
//                                                 ^^^^^^^^^^^^^^^^^^^^^^
dataSource = (DataSource) eventContext.lookup("jdbc/reportDataSource"); // JNDI lookup          
//                                             ^^^^^^^^^^^^^^^^^^^^^^

您要么直接查找:

dataSource =
  (DataSource) initialContext.lookup("java:comp/env/jdbc/reportDataSource");

或者您想首先获取上下文,但在这种情况下,您只需要java:comp/env

Context eventContext =
    (Context) initialContext.lookup("java:comp/env");
dataSource = (DataSource) eventContext.lookup("jdbc/reportDataSource");

答案 1 :(得分:0)

在Server.xml中,为资源提供名称并根据该名称执行查找。另一点是,您可以在webapps下的META-INF文件夹下添加资源作为新的context.xml。如果您不想更改server.xml,则可以执行此操作

<Resource name="tomcat/JDBCdatasource" auth="Container"  ... />

Context ctx;
ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");   
// Look up a data source
javax.sql.DataSource ds
      = (javax.sql.DataSource) envContext.lookup ("tomcat/JDBCdatasource");