- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我对 Hibernate (hibernate-core-4.1.9.Final.jar) 有疑问
案例 1:在 for 循环内进行 Hibernate 测试。一切顺利。
for(int i = 0; i < 1500; i++){
UserDAO.getInstance.getById(1);
}
案例 2:Thread.sleep() 内部循环。 1 分钟后出现异常。
for(int i=0; i<1500; i++){
UserDAO.getInstance.getById(1);
Thread.sleep(60000);
}
异常(exception):
00:20:06,447 WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08S01
00:20:06,448 ERROR SqlExceptionHelper:144 - Communications link failure
The last packet successfully received from the server was 120,017 milliseconds ago. The last packet sent successfully to the server was 9 milliseconds ago.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Communications link failure
The last packet successfully received from the server was 120,017 milliseconds ago. The last packet sent successfully to the server was 9 milliseconds ago.
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at sun.proxy.$Proxy11.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2031)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1832)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1811)
at org.hibernate.loader.Loader.doQuery(Loader.java:899)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2516)
at org.hibernate.loader.Loader.doList(Loader.java:2502)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
at org.hibernate.loader.Loader.list(Loader.java:2327)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1621)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:396)
at com.fit.utilities.BaseDAO.getById(BaseDAO.java:35)
at com.fit.test.Testiranje.main(Testiranje.java:51)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 120,017 milliseconds ago. The last packet sent successfully to the server was 9 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3603)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3492)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4043)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 17 more
Caused by: 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:3052)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3503)
... 29 more
这是我的hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://xxxxxx:3306/xxxx</property>
<property name="connection.username">xxxx</property>
<property name="connection.password">xxxx</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>
<property name="hibernate.connection.pool_size">1</property>
<mapping class="com.xxx.model.xxxxx" />
<mapping class="com.xxx.model.xxxxx" />
<mapping class="com.xxx.model.xxxxx" />
</session-factory>
</hibernate-configuration>
更新:使用 C3P0 库解决了这个问题。
<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->
最佳答案
问题的发生是因为 MySQL 服务器上的 time_out
变量值太小。
在我的情况下,time_out
设置为 1 分钟。使用C3PO
池机制我们可以优化JDBC
。
下载c3p0 -> http://sourceforge.net/projects/c3p0/
我正在使用 hibernate 3.0。
hibernate .cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://databasehost:3306/databasename</property>
<property name="connection.username">user</property>
<property name="connection.password">psw</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">false</property>
<!-- Hibernate c3p0 settings-->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">3</property>
<property name="hibernate.c3p0.idle_test_period">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">75</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.timeout">50</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!-- Mapping files -->
<mapping class="xxx.xxx.xxx.xxx" />
<mapping class="xxx.xxx.xxx.xxx" />
<mapping class="xxx.xxx.xxx.xxx" />
<mapping class="xxx.xxx.xxx.xxx" />
</session-factory>
</hibernate-configuration>
PersistenceManager.java
import java.io.PrintStream;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class PersistenceManager
{
private static SessionFactory sessionFactory = null;
private static PersistenceManager singleton = null;
public static PersistenceManager getInstance()
{
if (singleton == null)
{
singleton = new PersistenceManager();
}
return singleton;
}
public SessionFactory getSessionFactory()
{
if (sessionFactory == null)
createSessionFactory();
return sessionFactory;
}
protected void createSessionFactory()
{
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
}
public void destroySessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.close();
sessionFactory = null;
}
}
}
示例 1:
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
public Users Login( String username, String password)
{
Session session = null;
try
{
String hql = "select u from Users u where u.username like :p1 and u.password like :p2";
session = PersistenceManager.getInstance().getSessionFactory().openSession();
Query q = session.createQuery(hql)
.setParameter("p1", username)
.setParameter("p2", password);
if (q.list().size() == 0)
{
session.close();
return new Users();
}
Users user = (Users)q.list().get(0);
session.close();
return user;
}
catch (Exception e)
{
session.close();
}
}
示例 2:
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
public String Registration(Users u) {
Session session = null;
try
{
String hql = "select u from Users u where u.username like :p1";
session = PersistenceManager.getInstance().getSessionFactory().openSession();
Query q = session.createQuery(hql).setParameter("p1", u.getUsername());
if (q.list().size() == 0)
{
session.beginTransaction();
session.persist(u);
session.getTransaction().commit();
session.close();
return new Boolean(true).toString();
}
session.close();
return new Boolean(false).toString();
}
catch (Exception e)
{
return e.toString();
}
}
关于mysql - WARN SqlExceptionHelper :143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - 通信链接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15752667/
我对 Hibernate (hibernate-core-4.1.9.Final.jar) 有疑问 案例 1:在 for 循环内进行 Hibernate 测试。一切顺利。 for(int i = 0;
我想“捕捉这条消息” 2018-11-26 00:44:53.175 WARN 14548 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptio
由于其复杂性,使用 Hibernate JPA 在 Oracle DB 中执行 native 查询,我想捕获从 SqlExceptionHelper 类抛出的 "ORA-01722: Nombre n
错误 SqlExceptionHelper - 整数值不正确:'\xAC\xED\x00\x05sr\x00!com.domain.Client\xF3\xA5\x02\x0E\xC5d.\xA2\x
当列与数据库列不匹配时,我们在日志中收到如下异常。但它并没有抛出异常。我什至保留了 try catch。我怎样才能捕获异常? 警告 2015-11-04 17:25:44,055 [http-apr-
我有两个Grails域类(表)之间的Foriegn关键关系 class ServiceProvider { String name String address } class Fie
您好,我正在关注《行动》第 5 版书中的 spring,当我想从成分表中获取参数时遇到问题。在以下方法中,我尝试使用成分Repository.findAll() 填充列表: @GetMapping p
我的数据库中有一些表,我想获取有关对数据库的错误请求的信息。如果我试图用错误的外键保存实体,我想获取有关这些键的详细信息。 例如: 2020-03-25 18:37:37.595 ERROR 9788
我有一个托管在服务器(Tomcat 8.5)上的 Spring 应用程序。如果没有人使用它,它就会闲置。我已经知道如果数据库处于空闲状态8小时就会发生超时(MySQL的默认超时)。正如 Spring
我有以下域对象 @Table(uniqueConstraints={@UniqueConstraint(columnNames={"name", "company_id", "global"}, na
我正在尝试通过 flume 将使用聚集列存储索引的 DWH SQL Server 表中的数据导入到 kudu 中。但是,在我的自定义 flume 源从数据库中检索一定数量的行后,出现以下异常: Sql
我有一个 spring 应用程序,我有一个使用以下语法的 native 查询: select COUNT(DISTINCT person.id,(CASE WHEN salary_perso
我有一个 Spring 3.1、Hibernate 4 和 Primefaces 应用程序。它在我的本地计算机上运行良好,但是当我在我的 Web 主机上部署 ant 生成的 war 文件时,当我尝试登
我是 Java EE 新手,当我尝试通过 WEB-APP 从 HSQL DB 获取数据时,我在 Wildfly 控制台中收到此错误。 20:11:53,780 INFO [stdout] (defa
请帮忙。我是 hibernate 5 + MySQL 的新手,所以这可能是个愚蠢的场景。 我正在尝试创建一个非常基本的社交媒体应用程序,允许注册、登录和添加 friend 。我收到此错误: 23:47
在这里,我试图找到所有在我提供的范围内的实体。我的意思是,如果我给出一个特定半径的圆,它将必须显示位置坐标位于给定圆内的所有实体。 我正在使用 hibernate-spatial 来实现这一点。但是在
我最近将Grails应用程序部署到Tomcat,并在启动时遇到以下异常。 o.h.engine.jdbc.spi.SqlExceptionHelper 我不确定发生了什么。在BootStrap中创建所
下面是我在 ORACLE 中的查询,它出现了 ORA-00917:缺少逗号错误。 public boolean setFileDetails(String fileName,String fileTy
我已经尝试解决这个问题两天了,但没有任何效果。我将这些实体隐含在双向关系中。 @Entity @Table( name = "T_user" ) public class T_user impleme
我在尝试通过 hibernate 返回多个实体时遇到错误。根据文档我尝试了以下选择语句: sess.createSQLQuery("SELECT {a.*}, {b.*} FROM A a LEFT
我是一名优秀的程序员,十分优秀!