Ms Access を使用して Connection Pooling をテストしようとしていますが、うまくいきません。また、Stackoverflow などで同様の問題を読みましたが、まだ問題を解決できません。
Tomcat のエラー コードは次のとおりです。
javax.naming.NameNotFoundException: Name PracticalODBC1 is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at ProcessRegistration.init(ProcessRegistration.java:37)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
ここに私のcontext.xmlがあります:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/odbc:PracticalODBC1" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc://localhost:3306/pract1"
maxActive="10" maxIdle="4" />
そして私のサーブレット:
そして私の DoPost サーブレット:
public class ProcessRegistration extends HttpServlet {
private DataSource dataSource;
private Connection conn;
private Statement stmt;
DataSource pool;
@Override
public void init() throws ServletException {
try {
// Get DataSource
Context initContext = new InitialContext();
pool = (DataSource) initContext.lookup("java:/comp/env/PracticalODBC1");
if (pool == null) {
throw new ServletException("Unknown DataSource 'jdbc/PracticalODBC1'");
}
} catch (NamingException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
stmt = null;
try {
conn = pool.getConnection();
stmt = conn.createStatement();
// inserting records into database
-----
} catch (SQLException ex) {
Logger.getLogger(ProcessRegistration.class.getName()).log(Level.SEVERE, null,ex);
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
throw new ServletException("Servlet records", e);
}
}
out.close();
MySQL Connector jar はすでに lib に配置されています。
何がうまくいかなかったのか、誰かが親切にアドバイスしてくれることを願っています。