random Error calling Connection.setAutoCommit

211 views
Skip to first unread message

PeiSong

unread,
Jul 30, 2010, 3:40:35 AM7/30/10
to mybati...@googlegroups.com
Hi,

I use glassfish JNDI, but randomly got this exception: 
[#|2010-07-30T15:30:44.346+0800|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=31;_ThreadName=Thread-1;|ERROR: java.sql.Connection - Error calling Connection.setAutoCommit:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 52,541 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
at sun.reflect.GeneratedConstructorAccessor829.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:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4956)
at com.mysql.jdbc.jdbc2.optional.ConnectionWrapper.setAutoCommit(ConnectionWrapper.java:155)
at com.sun.gjc.spi.base.ConnectionHolder.setAutoCommit(ConnectionHolder.java:624)
at sun.reflect.GeneratedMethodAccessor1503.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:52)
at $Proxy165.setAutoCommit(Unknown Source)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:42)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.<init>(JdbcTransaction.java:15)
at org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory.newTransaction(JdbcTransactionFactory.java:15)
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77)
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)
at asia.glbt.dev.dao.mybatis.BaseDao.getSession(BaseDao.java:15)
at asia.glbt.dev.dao.mybatis.BaseDao.getMapper(BaseDao.java:19)
at asia.glbt.dev.dao.mybatis.ProfileDao.getTopHeartedUsersByCountryWithPage(ProfileDao.java:21)
at asia.glbt.dev.service.common.db.ProfileService.getTopHeartedUsersByCountryWithPage(ProfileService.java:128)
at asia.glbt.dev.web.admin.controller.HomepageController.getTopHeartedUsers(HomepageController.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:820)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:517)
at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:488)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:336)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:314)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
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:2502)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2952)
... 80 more
|#]

My config is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

<configuration>
<typeAliases>
<typeAlias type="asia.glbt.dev.model.pojo.User" alias="User"></typeAlias>
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="JNDI">
<property name="data_source" value="jdbc/db1Pool" />
</dataSource>
</environment>
<environment id="stage">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.1.5:3306/fridaeweb" />
<property name="username" value="root" />
<property name="password" value="kylie" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/ProfileMapper.xml" />
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/HeartMapper.xml" />
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/UserMapper.xml" />
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/LocationInfoMapper.xml" />
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/PictureMapper.xml" />
<mapper resource="asia/glbt/dev/dao/mybatis/mapping/PerksMapper.xml" />
</mappers>
</configuration>

Can help? Thank you.

PeiSong

PeiSong

unread,
Jul 30, 2010, 4:39:07 AM7/30/10
to mybati...@googlegroups.com

Here is the application error log:

 

DEBUG: java.sql.Connection - xxx Connection Closed

INFO : asia.glbt.dev.dao.mybatis.BaseDao - session closed.

DEBUG: asia.glbt.dev.dao.mybatis.ProfileDao - ** get top hearted users by country & page

DEBUG: asia.glbt.dev.web.admin.controller.HomepageController - getting top hearted users.

DEBUG: java.sql.Connection - ooo Connection Opened

ERROR: java.sql.Connection - Error calling Connection.setAutoCommit:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

 

The last packet successfully received from the server was 143,594 milliseconds ago.  The last packet sent successfully to the server was 2 milliseconds ago.

                at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

                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:409)

                at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)

                at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055)

                at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941)

                at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)

                at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)

                at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)

                at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)

                at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4956)

                at com.mysql.jdbc.jdbc2.optional.ConnectionWrapper.setAutoCommit(ConnectionWrapper.java:155)

                at com.sun.gjc.spi.base.ConnectionHolder.setAutoCommit(ConnectionHolder.java:624)

                at sun.reflect.GeneratedMethodAccessor179.invoke(Unknown Source)

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

                at java.lang.reflect.Method.invoke(Method.java:597)

                at org.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:52)

                at $Proxy105.setAutoCommit(Unknown Source)

                at org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:42)

                at org.apache.ibatis.transaction.jdbc.JdbcTransaction.<init>(JdbcTransaction.java:15)

                at org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory.newTransaction(JdbcTransactionFactory.java:15)

                at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:77)

                at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:32)

                at asia.glbt.dev.dao.mybatis.BaseDao.getSession(BaseDao.java:15)

                at asia.glbt.dev.dao.mybatis.BaseDao.getMapper(BaseDao.java:19)

                at asia.glbt.dev.dao.mybatis.ProfileDao.getTopHeartedUsersByCountryWithPage(ProfileDao.java:21)

                at asia.glbt.dev.service.common.db.ProfileService.getTopHeartedUsersByCountryWithPage(ProfileService.java:128)

                at asia.glbt.dev.web.admin.controller.HomepageController.getTopHeartedUsers(HomepageController.java:36)

                at sun.reflect.GeneratedMethodAccessor190.invoke(Unknown Source)

                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

                at java.lang.reflect.Method.invoke(Method.java:597)

                at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)

                ... 79 more

ERROR: asia.glbt.dev.dao.mybatis.ProfileDao - Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

PeiSong

unread,
Jul 30, 2010, 12:35:52 PM7/30/10
to mybati...@googlegroups.com
After a lot of test and reading, I make it by enabling validation of connection.

But I still think the problem is caused by the mysql server, which set the idle, wait timeout too short for clients to stay connected. The clients think it's still alive, but the mysql server has closed it. Will do more test next week.

François Schiettecatte

unread,
Jul 30, 2010, 1:17:27 PM7/30/10
to mybati...@googlegroups.com
I got exactly the same error a while back when upgraded from one release of myBatis 3.x to the next, I spent two days tracking it down and it automagically went away when I rebooted the machine I was working on (which also rebooted mysql). I have not had the issue since. You will probably also want to check the timeouts in mysql and in your connection pool.

I have the mysql timeouts set thus:

# Connection timeouts
connect_timeout = 10
wait_timeout = 60
interactive_timeout = 3600


and my c3p0 connection pool timeout set thus:

<property name="maxIdleTime" value="50"/>
<property name="maxIdleTimeExcessConnections" value="50"/>

the theory being that c3p0 will drop idle connection from the pool before mysql closed it.

Cheers

François

PeiSong

unread,
Jul 31, 2010, 12:41:13 AM7/31/10
to mybati...@googlegroups.com
Tks.

The wait_timeout = 60 means if there is no action in 60 seconds the connection will also be closed. So I'm wondering how those connection remains.

If so, the connection should do sth like select system variable every a few seconds right?

PeiSong

unread,
Jul 31, 2010, 1:26:36 AM7/31/10
to mybati...@googlegroups.com
Oh I get it.
wait_timeout applies for tcp/ip connections.
so here wait_timeout rules.

also from glassfish wiki:

idle-timeout-in-seconds
maximum time in seconds, that a connection can remain idle in
the pool. After this time, the pool implementation can close
this connection. ===>Note that this does not control connection
timeouts enforced at the database server side<=======. Adminsitrators
are advised to keep this timeout shorter than the EIS
connection timeout (if such timeouts are configured on the
specific EIS), to prevent accumulation of unusable connection
in Application Server.

François Schiettecatte

unread,
Jul 31, 2010, 9:39:38 AM7/31/10
to mybati...@googlegroups.com
Just to clarify and expand...

You have two sets of timeouts, one at the mysql end which are defined in /etc/my.cnf, there are the ones I use:

# Connection timeouts
connect_timeout = 10
wait_timeout = 60
interactive_timeout = 3600

The important one is the wait_timeout which is the amount of time mysql idles before dropping the connection automatically, in this case 60 seconds. The connect_timeout is the amount of time mysql waits for a user name/password upon connection, and the interactive_timeout is for interactive apps like command line mysql. There are other network level timeouts but these are out of scope. You can get a lot of information about all this here:

http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html
http://dev.mysql.com/doc/refman/5.1/en/server-options.html
http://dev.mysql.com/doc/refman/5.1/en/option-files.html

At the other end, there are the connection pool timeout and how you define these is pool specific. I use C3P0 because I like it. I had to write a small amount of code to pull configuration values out of the ibatis xml configuration file, the two settings that C3P0 uses which are important to us are:

<property name="maxIdleTime" value="50"/>
<property name="maxIdleTimeExcessConnections" value="50"/>

This tells C3P0 to drop idle connections after 50 seconds, pretty much ensuring that these will be dropped before mysql drops them causing the error you see. I did some testing and 50 seconds seemed to be the value closest to 60 I could use without causing the error you see.

You can read more about the C3P0 settings here:

http://www.mchange.com/projects/c3p0/

I don't know which connection pool you are using but I expect you will find similar settings to control the connection timeouts, I used DBCP beforehand and I recall it worked pretty much the same way.

If you want to get fancy you can also use some sort of 'ping' to keep idle connections alive (using a query such as 'select 1' on mysql). This would make sense in an environment where opening a connection is expensive, but this is not the case with mysql so I don't bother.


Cheers

François

Reply all
Reply to author
Forward
0 new messages