这是我的context.xml文件:
... <Resource auth="Container" driverClass="net.sourceforge.jtds.jdbc.Driver" type="com.jolBox.bonecp.BoneCPDataSource" idleMaxAge="240" idleConnectionTestPeriod="60" partitionCount="3" acquireIncrement="1" maxConnectionsPerPartition="10" minConnectionsPerPartition="3" statementsCacheSize="50" releaseHelperThreads="4" name="jdbc/MyDatasource" username="my_username" password="my_password" factory="org.apache.naming.factory.beanfactory" jdbcUrl="jdbc:jtds:sqlserver://localhost:12345/my_database" /> ...
我已经尝试使用ServletContext.getResource(java.lang.String)与资源的名称(“jdbc / MyDatasource”),但是Tomcat抱怨该名称不以’/’开头.我也试过用“/ jdbc / MyDatasource”,但这次它返回null.
我主要需要jdbcUrl与数据库服务器进行连接检查(请参阅服务器是否处于联机状态).
解决方法
关键字是:JNDI. context.xml中的资源不是“系统资源”,而是JNDI资源.
尝试这个:
尝试这个:
InitialContext ic = new InitialContext(); // that's everything from the context.xml and from the global configuration Context xmlContext = (Context) ic.lookup("java:comp/env"); DataSource myDatasource = (DataSource) xmlContext.lookup("jdbc/MyDatasource"); // now get a connection to see if everything is fine. Connection con = ds.getConnection(); // reaching this point means everything is fine. con.close();