Saturday, January 31, 2009

Meanwhile: Hibernate / mySQL Problems: 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(
at org.hibernate.exception.JDBCExceptionHelper.convert(
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:


MESSAGE: Communications link failure

Last packet sent to the server was 1442 ms ago.


com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 1442 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(
Caused by: 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(

at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(

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- from liferay. (Again you should by able to add it to your server and reference it via the 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. :)


  1. This comment has been removed by the author.

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

    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

    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

  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

  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?

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

  6. Hey! :)

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

    For me that is:

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

    Hope that helps!

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

  8. I had to add property connection.provider_class as org.hibernate.connection.C3P0ConnectionProvider
    otherwise it wouldn't pick 3dp0 up, as stated here (

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

  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

  11. It was so bravely and depth information. I feel so good read to your blog. I would you like thanks for your posting such a wonderful blog!!!
    SEO Training in Chennai Velachery
    SEO Course in Tnagar
    SEO Training in Tnagar
    SEO Classes near me
    SEO Training in Karappakkam
    SEO Training in Navalur

  12. wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    This article resolved my all queries.good luck an best wishes to the team members.continue posting.learn digital marketing use these following link
    Digital Marketing Course in Chennai


I'm happy about every feedback! :)