mysql几小时后自动关闭服务!我用tomcat发布的网站,mysql数据库,连数据库用hibernate!一段时间后不好使
Messages:Cannotreadresponsefromserver.Expectedtoread4bytes,read0bytesbeforeconnection...
Messages: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
Communications link failure The last packet successfully received from the server was 34,497,469 milliseconds ago. The last packet sent successfully to the server was 16 milliseconds ago.
No operations allowed after connection closed.Connection was implicitly closed by the driver.
could not execute query 展开
Communications link failure The last packet successfully received from the server was 34,497,469 milliseconds ago. The last packet sent successfully to the server was 16 milliseconds ago.
No operations allowed after connection closed.Connection was implicitly closed by the driver.
could not execute query 展开
展开全部
这个要看你的Hibernate代码了,有没有定时刷新缓存,比如 session.flush()。大数据量的update会吃光hibernate的缓存,然后你的数据库服务器就挂掉了,要批量更新,更新一次,删一次缓存,这样服务器内存就不会被吃光挂掉了。
追问
只用session.getTranssion.submit()不行吗
追答
session.getTranssion.commit()事务提交。每次commit之后,session中的entity会与数据库同步。问题就在这里,如果你的数据操作简单,那么session中的entity不多,当然就没事。如果
Session session = getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from bkch2 where name like ?");
query.setString(0, "%Hibernate%");
List books = query.list();
for (Iterator iter = books.iterator(); iter.hasNext();)
{
BookCh2 book = (BookCh2) iter.next();
book.setPrice(new Integer(book.getPrice()- 10));
session.saveOrUpdate(book);
}
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
} finally {
session.close();
}
List books = query.list();这句,如果这个list足够多,session缓存就会被吃光,你的服务器就挂了。
Session session = getSession();
Transaction tx = null;
tx = session.beginTransaction();
ScrollableResults books = session.createQuery("from bkch2 where name like 'Book%'")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while ( books.next() ) {
BookCh2 book = (BookCh2) books.get(0);
book.setPrice(new Integer(book.getPrice()- 10));
if ( ++count % 25 == 0 ) {
//flush a batch of updates and release memory:
System.out.println("Flushing in batches");
session.flush();
session.clear();
}
}
tx.commit();
session.close();
这段代码就是它没有直接取出所有的result,ScrollableResults books = session.createQuery("from bkch2 where name like 'Book%'")
.setCacheMode(CacheMode.IGNORE)
.scroll(ScrollMode.FORWARD_ONLY);
if ( ++count % 25 == 0 ) {
//flush a batch of updates and release memory:
System.out.println("Flushing in batches");
session.flush();
session.clear();
每操作了25个,clear下session,这样就不会吃光内存了。
看下hibernate 的文档,中有这部分的描述,写代码的时候要注意的。
查查你自己的代码有没有批量操作的问题。还有没有把session,close掉,session漏关也会内存泄漏。只能说到这里了,因为没看过你的代码,纯粹是猜测
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询