Saturday, January 31, 2009

Meanwhile: Hibernate / mySQL Problems: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes....

Hi all,

we're almost there.
My "user generated content" portlets are running (EasyBlogEntry and EasyComments) - but as expected: There are unforeseen problems. :)

Since I deployed the Hibernate3 based portled EasyComments I get the following exception always after the liferay server is running for some time - roughly every second day. (I use mySQL 5.0.22-0ubuntu6.06.5)


10:53:20,430 ERROR [jsp] java.lang.Exception: org.hibernate.exception.JDBCConnectionException: could not execute query
...
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)
...
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:


** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure

Last packet sent to the server was 1442 ms ago.

STACKTRACE:


com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 1442 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2873)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2763)
...
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:2332)

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


After quite some research in the web I got different clues about which jdbc driver to use, what mysql parameters to set and how to configure hibernate.

At first I harmonized my mySQL drivers (liferay portal had mysql-connector-java-5.0.8-bin.jar and in my portlet mysql-connector-java-5.1.5-bin.jar for some reason. (I should use the one shared library here anyway - well next time..))
Anyway: I didn't help. (I could image that it took longer until the error - but it still kept coming)

As suggested by the mySQL forum I tried now to use c3p0 as the connection pool.
I achieved this by:

1. configuring the hibernate.cfg.xml file

simply add the following to your file:

<!-- Sessions and transactions -->
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="current_session_context_class">thread</property>



As I did here:



Additionally you have to add the c3p0.jar to your portlet! I took the one from the sample portlet sample-dao-portlet-5.1.1.1.war from liferay. (Again you should by able to add it to your server and reference it via the liferay-plugin-package.properties file - but I just put in the /lib folder at first for the sake of ease.)

Unfortunately I have to wait now for at least three days to see if it really helps. Of course I will update you folks with the result. So come back and find out. :)

13 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Update: Connections are still running - solution works! :)

    ReplyDelete
    Replies
    1. Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

      Delete
    2. Java Training Institutes Java Training Institutes
      Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

      Delete
  3. Hi,

    I need to use Liferay 5.1.2 and need to use c3p0 to connect to database by using connectionCustomizerClassName property. I need this for the entire portal not only a portlet. Can you please guide how to configure c3p0 for Liferay 5.1.2

    Thanks in advance

    ReplyDelete
  4. Hi,

    thanks for your question.

    why do you need to go with that solution? Maybe there is another way?

    Since liferay 5.2 c3p0 is the default connection pool as I understand.

    I'm not sure how to configure whole liferay for c3p0 in that version.

    Maybe somebody knows?

    ReplyDelete
  5. What path is the hibernate.cfg.xml file supposed to go into? And does portal-ext.properties need to configured to point to it?

    ReplyDelete
  6. Hey! :)

    The hibernate.cfg.xml should go to the root folder of your code - same place where you put the language.properties files.

    For me that is:
    docroot/WEB-INF/src

    The portal-ext.properties has nothing to do with that (as the portal-ext is not portlet specific)

    Hope that helps!

    ReplyDelete
  7. I faced the same issue. Tried to configure the c3p0 with this link

    http://hibernatedb.blogspot.com/2009/05/automatic-reconnect-from-hibernate-to.html

    ReplyDelete
  8. I had to add property connection.provider_class as org.hibernate.connection.C3P0ConnectionProvider
    otherwise it wouldn't pick 3dp0 up, as stated here (https://forum.hibernate.org/viewtopic.php?p=2399313)

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. I share it this blog information really useful to me and I'm waiting for our other blog update. I give that suggestion to you just update the blog now correct technologies of data analytics, digital marketing and testing relevant blogs.
    SEO Training in Chennai

    ReplyDelete

I'm happy about every feedback! :)