mysql数据库连接池连接成功org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFException
MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.STACKTRACE:java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at org.apache.tomcat.dbcp.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.apache.commons.dbutils.QueryRunner.prepareConnection(QueryRunner.java:195)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:306)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:328)
at cn.com.jobedu.blog.HomeServlet.main(HomeServlet.java:80)
at cn.com.jobedu.blog.HomeServlet.doGet(HomeServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
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:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Unknown Source)
DbHelper.javapublic class DbHelper {
public static QueryRunner getQueryRunner() {
//数据源对象可以理解为连接池的管理者,通过他可以获取数据库的连接
DataSource ds = null;
try {
//通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
Context context = new InitialContext();
ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
System.out.println("mysql数据库连接池连接成功");
} catch (Exception e) {
System.out.println("获取数据源时出错");
} QueryRunner qr = new QueryRunner(ds);

return qr;
}
}
上面已经提示我可以连接上连接池了,但后面又报异常,什么连接工厂~~public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = -7152478870507997462L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String method = request.getParameter("method"); if (method == null) {
main(request, response);
request.getRequestDispatcher("/main.jsp")
.forward(request, response);
} else if (method.equals("get")) {
main(request, response);
get(request, response);
request.getRequestDispatcher("/displayBlog.jsp").forward(request,
response);
}
} public void get(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id = request.getParameter("id");
String sql = "select id,title,content,createdtime from blog where id="
+ id;
QueryRunner qr = DbHelper.getQueryRunner();

Blog blog = null;
try {
List list = (List) qr.query(sql, new BeanListHandler(Blog.class));
blog = (Blog) list.get(0);
} catch (SQLException e) {
e.printStackTrace();
} // 根据博文的id,读取这个博文下的所有评论
sql = "select id,username,content,createdtime from comment where blog_id="
+ id + " order by id desc";
List commentList = null;
try {
commentList = (List) qr.query(sql, new BeanListHandler(
Comment.class));
} catch (SQLException e) {
e.printStackTrace();
} request.setAttribute("blog", blog);
request.setAttribute("commentList", commentList);

} public void main(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String cid=request.getParameter("cid");
String sql=null;
if(cid==null){
sql = "select b.id as id,title,content,createdtime,name as category,c.id as categoryid  from blog b,category c where  category_id=c.id order by b.id desc  limit 0,4";
}else{
sql = "select b.id as id,title,content,createdtime,name as category,c.id as categoryid  from blog b,category c where  category_id=c.id and category_id="+cid+" order by b.id desc  limit 0,4";
}

// DButils中核心类,生成对象时传递数据源对象
QueryRunner qr = DbHelper.getQueryRunner(); // 查询最新博文  
List blogs = null;
try {
blogs = (List) qr.query(sql, new BeanListHandler(Blog.class));
} catch (SQLException e) {
e.printStackTrace();
}
// 查询分类
sql = "select id,name from category order by level desc,id desc";
List categorys = null;
try {
categorys = (List) qr.query(sql,
new BeanListHandler(Category.class));
} catch (SQLException e) {
e.printStackTrace();
}
// 查询最新的评论
sql = "select id,username,content,blog_id as blogid from comment order by id desc limit 0,4";
List comments = null; try {
comments = (List) qr.query(sql, new BeanListHandler(Comment.class));
} catch (SQLException e) {
e.printStackTrace();
}
// 读取首页需要显示的博文
request.setAttribute("blogs", blogs);
// 读取首页需要显示的分类信息
request.setAttribute("categorys", categorys);
// 读取首页需要显示的评论
request.setAttribute("comments", comments);
}
}编译器卡在这了类的处理上

解决方案 »

  1.   

    Can not read response from server.  无法获取服务器返回的信息啊。
      

  2.   

     at org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)从异常堆栈看是无法创建池化连接工厂对象nested exception是
    Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.这个原因肯定很多,他这提示没说出为啥unexpectedly lost,搜一下上面这句也能搜到很多的
      

  3.   

    一种方式:
    解决方法:在data source配置时加入以下三行即可解决问题:
    <property name="testConnectionOnCheckin" value="true" /> 
      <property name="testConnectionOnCheckout" value="true" /> 
      <property name="idleConnectionTestPeriod" value="18000" />
    二种方式:
    用hibernate的话, 加如下属性: 
    <property name="connection.autoReconnect">true</property> 
    <property name="connection.autoReconnectForPools">true</property> 
    <property name="connection.is-connection-validation-required">true</property>