我遇到的问题是我们只能在生产服务器上重现。 这是堆栈跟踪:
Caused by: org.hibernate.exception.JDBCConnectionException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2214)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at com.dir.web.beans.clientpartners.dao.DaoClientPartnerImpl.getBrandAssignments(DaoClientPartnerImpl.java:268)
... 46 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.GeneratedConstructorAccessor143.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3134)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1818)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
... 53 more
Caused by: java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3119)
... 64 more
您可以看到抛出异常的代码:
public List<BrandAssignment> getBrandAssignments(String query) throws ClientPartnerException {
Validate.notEmpty(query, "The query cannot be empty or null");
List<BrandAssignment> brandAssignments = null;
try {
brandAssignments = getCurrentSession().createSQLQuery(query).addEntity(BrandAssignment.class).list();
getCurrentSession().close();
} catch (HibernateException hibernateException) {
throw new ClientPartnerException("Error getting brand assignments with query: " + query, hibernateException);
}
return brandAssignments;
}
getCurrentSession方法基本上向会话工厂询问当前会话,如果之前没有人启动它,则开始一个事务。
这是我对Hibernate的配置:
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">5</property>
<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>
<property name="current_session_context_class">thread</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
查询如下所示:
SELECT DISTINCT brand_assig.brand_assignment_id, brand_assignment_name, brand_assig.date_created, brand_assig.date_modified, brand_assig.modified_by, brand_assig.client_division_id, brand_assig.business_category, brand_assig.country, brand_assig.region, brand_assig.office_alias, brand_assig.assignment_start_date, brand_assig.assignment_end_date, brand_assig.assignment_status, brand_assig.industry_experience, brand_assig.description, brand_assig.communication_disciplines, brand_assig.client_type
FROM clnt_parent INNER JOIN clnt_partner INNER JOIN clnt_div INNER JOIN brand_assig
ON client_division.client_division_id = brand_assig.client_division_id AND client_partner.client_partner_id = client_division.client_partner_id AND client_parent.client_parent_id = client_partner.client_parent_id
WHERE ( MATCH (client_partner_name) AGAINST ('+nutrition*'IN BOOLEAN MODE) OR MATCH (client_parent_name) AGAINST ('+nutrition*'IN BOOLEAN MODE) OR MATCH (client_division_name) AGAINST ('+nutrition*'IN BOOLEAN MODE) OR MATCH (brand_assignment_name, industry_experience) AGAINST ('+nutrition*'IN BOOLEAN MODE)) AND brand_assig.assignment_end_date IS NOT NULL
ORDER BY client_partner.client_partner_name
问题是我无法在开发环境中重现错误以测试解决方案,而且我不确定是什么导致问题,因为我检查配置并且似乎没问题。 查看为MySql服务器设置的超时默认设置为8小时。 错误在prod中经常发生,但从不在开发环境中发生。 任何人都知道可能导致错误的原因以及如何重现? 任何可能的解决方 谢谢!