JDBC 连接错误
JDBC connection error
我在 Glassfish 中创建了一个 JDBC 连接池并将其用作 JDBC 资源 "jdbc/__default"。我可以在管理控制台界面成功ping通这个连接池。
然后,我正在尝试运行以下程序:
package de.java2enterprise.onlineshop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// @Resource(name="jdbc/__default")
// private DataSource ds;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
writer.println("<!DOCTYPE html>");
writer.println("<html><body>");
Connection con = null;
DataSource ds;
try {
ds = (DataSource) InitialContext.doLookup("jdbc/__default");
con = ds.getConnection();
if (con.isValid(10)) {
writer.println("<BR>Connected!");
}
con.close();
} catch (Exception ex) {
writer.println(ex.getMessage());
} finally {
if (con != null){
try{
con.close();
} catch (Exception ex){
}
}
}
writer.println("<BR>Test finished!</body></html>");
writer.close();
}
}
但是,我在网络浏览器中收到以下错误:
Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12518, TNS:listener could not hand off client connection
Test finished!
在服务器日志中,我收到以下错误:
2015-04-08T08:15:20.540+0200|Information: Loading application [onlineshop#onlineshop-war.war] at [onlineshop-war]
2015-04-08T08:15:20.636+0200|Information: onlineshop was successfully deployed in 611 milliseconds.
2015-04-08T08:15:38.552+0200|Warnung: Context path from ServletContext: /onlineshop-war differs from path from bundle: onlineshop-war
2015-04-08T08:15:38.802+0200|Information: visiting unvisited references
2015-04-08T08:15:39.519+0200|Information: Successfully got INSTRUMENTATION: sun.instrument.InstrumentationImpl@2d618042
2015-04-08T08:15:41.471+0200|Warnung: RAR5038:Unexpected exception while creating resource for pool Onlineshop. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.471+0200|Warnung: RAR5117 : Failed to obtain/create connection from connection pool [ Onlineshop ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.486+0200|Warnung: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
]
之后,我无法再次 ping 连接池,并在管理控制台中收到或多或少相同的错误。
我使用以下配置:JDK 1.8.0_25,GlassFish Server 开源版 4.1(内部版本 13),Oracle 数据库 XE 11.2。
在 Glassfish 管理控制台中,我创建了以下 JDBC 资源:JNDI 名称:jdbc/__default,逻辑 JNDI 名称:java:comp/DefaultDataSource, 连接池: Onlineshop.
JDBC 连接池名称为 Onlineshop,资源类型:javax.sql.DataSource,类名:oracle.jdbc.pool.OracleDataSource。
我的最小池大小为 800 个连接,最大为 3200 个连接。我保留的其他参数,我只在 JDBC 连接池属性中添加了用户和密码。
有人知道吗? :-)
如果您使用 jdk7+,请在 finally block or in try-with-resources 中关闭您的连接,因为它应该在 Java 中完成。看看有没有帮助
我在 Glassfish 中创建了一个 JDBC 连接池并将其用作 JDBC 资源 "jdbc/__default"。我可以在管理控制台界面成功ping通这个连接池。
然后,我正在尝试运行以下程序:
package de.java2enterprise.onlineshop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
@WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// @Resource(name="jdbc/__default")
// private DataSource ds;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
writer.println("<!DOCTYPE html>");
writer.println("<html><body>");
Connection con = null;
DataSource ds;
try {
ds = (DataSource) InitialContext.doLookup("jdbc/__default");
con = ds.getConnection();
if (con.isValid(10)) {
writer.println("<BR>Connected!");
}
con.close();
} catch (Exception ex) {
writer.println(ex.getMessage());
} finally {
if (con != null){
try{
con.close();
} catch (Exception ex){
}
}
}
writer.println("<BR>Test finished!</body></html>");
writer.close();
}
}
但是,我在网络浏览器中收到以下错误:
Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12518, TNS:listener could not hand off client connection Test finished!
在服务器日志中,我收到以下错误:
2015-04-08T08:15:20.540+0200|Information: Loading application [onlineshop#onlineshop-war.war] at [onlineshop-war] 2015-04-08T08:15:20.636+0200|Information: onlineshop was successfully deployed in 611 milliseconds. 2015-04-08T08:15:38.552+0200|Warnung: Context path from ServletContext: /onlineshop-war differs from path from bundle: onlineshop-war 2015-04-08T08:15:38.802+0200|Information: visiting unvisited references 2015-04-08T08:15:39.519+0200|Information: Successfully got INSTRUMENTATION: sun.instrument.InstrumentationImpl@2d618042
2015-04-08T08:15:41.471+0200|Warnung: RAR5038:Unexpected exception while creating resource for pool Onlineshop. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.471+0200|Warnung: RAR5117 : Failed to obtain/create connection from connection pool [ Onlineshop ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.486+0200|Warnung: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack ]
之后,我无法再次 ping 连接池,并在管理控制台中收到或多或少相同的错误。
我使用以下配置:JDK 1.8.0_25,GlassFish Server 开源版 4.1(内部版本 13),Oracle 数据库 XE 11.2。
在 Glassfish 管理控制台中,我创建了以下 JDBC 资源:JNDI 名称:jdbc/__default,逻辑 JNDI 名称:java:comp/DefaultDataSource, 连接池: Onlineshop. JDBC 连接池名称为 Onlineshop,资源类型:javax.sql.DataSource,类名:oracle.jdbc.pool.OracleDataSource。
我的最小池大小为 800 个连接,最大为 3200 个连接。我保留的其他参数,我只在 JDBC 连接池属性中添加了用户和密码。
有人知道吗? :-)
如果您使用 jdk7+,请在 finally block or in try-with-resources 中关闭您的连接,因为它应该在 Java 中完成。看看有没有帮助