我在制作中有页面,有时访问主页时会出现此错误:
java.net.SocketException: Broken pipe (Write failed)
java.net.SocketOutputStream.socketWrite0(Native Method)
java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
java.net.SocketOutputStream.write(SocketOutputStream.java:155)
java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3626)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2452)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2546)
com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4873)
org.hibernate.engine.jdbc.connections.internal.PooledConnections.poll(PooledConnections.java:84)
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:186)
org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
...
导致了这个:
org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
同样在我的开发环境中,当我重启tomcat时会收到此警告:
WARNING: The web application [myApplication] appears to have started a thread named [pool-5-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
...
这是我的persistance.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myApplication">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myApplication?autoReconnect=true&useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.schema-generation.database.action"
value="create" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
我确定在关闭EntityManager的地方,我打开了一个:
我还尝试添加连接池:
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
但我仍然有这两个问题!
我读到Broken管道异常是由客户端尝试使用已关闭的连接引起的,但在涉及的查询中,我获得了一个新的实体管理器。
@RequestMapping("/")
public String showHomePage(Model model, HttpSession session) {
if (session.getAttribute("cart") == null) {
session.setAttribute("cart", new ShoppingCart());
}
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories", categories);
List<Product> products = productService.findAllSellableProducts();
for (Product product : products) {
product.setInventories(inventoryService.findInvenoriesByProduct(product));
}
model.addAttribute("products", products);
return "index2";
}
public List<Category> findAllCategories() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
List<Category> categories = categoryDAO.findAllCategories(em);
em.getTransaction().commit();
return categories;
} catch (Exception e) {
throw e;
} finally {
em.close();
}
}
希望有人能看到我在这里做错了什么。
感谢