If you are using MySQL and Hibernate, maybe you are familar with an exception like this one:


com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was79095 seconds ago.The last packet sent successfully to the server was 79095 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

To me, this problem occurred whenever my web application was not used for a certain amount of time. For some reason, Hibernate (?) does not close the connection properly, but the database system does after the wait_timeout has passed. Now Hibernate wants to use a connection that is not valid any more.

It took me some effort to overcome this problem, but I figured out a clean solution (setting the ‘wait_timeout’ setting of mysqld to days or weeks is NOT a good approach ;)).

The trick is to use connection pooling with c3p0. This way, you can make sure that a connection is revalidated after being idle for a long time. Configuration is quite easy. Just add the following properties to your Hibernate configuration (persistence.xml or hibernate.cfg.xml):

<property name="hibernate.c3p0.min_size" value="3" />
<property name="hibernate.c3p0.max_size" value="50" />
<property name="hibernate.c3p0.timeout" value="1800" />
<property name="hibernate.c3p0.max_statements" value="50" />
<!– this property forces the revalidation of a connection after the given amount of time (in secs) –>
<!– it must be set to LESS than the wait_timout setting for the mysql server (this setting defaults to 28800 secs (8 hours)) –>
<property name="hibernate.c3p0.idle_test_period" value="28680" />

Furthermore, you have to add the c3p0 jar to the classpath of your application.

Today I want to introduce my new project Cloud42, an Open Source management framework for Cloud Computing with Amazon EC2.
This is not intended to be a dumb advertisement. Instead, the main cause for this post here is that Cloud42 is very interesting for developers and therefore can be of value for you, too.

Cloud42 is a web application written in Java. It provides both a AJAX-enabled GUI and an extensive Web service interface, allowing you to invoke its functionalities from your own application or from within BPEL processes.

Besides the basic functionalities like starting, stopping and monitoring EC2 AMI instances, Cloud42 offers some enhanced functions like transferring files and bundling new AMIs. Furthermore, it is possible to control your instances remotely by sending arbitrary commands through the Web service interface (or by using the GUI). A notification mechanism following the publish/subscribe pattern allows you to subscribe any endpoint to events that occur on an AMI instance.

This sounds interesting? Then visit the website at cloud42.net!
And don’t forget to drop a comment here!

I’ve had an interesting problem with FileOutputChannel and RandomAccessFile from Java NIO.

If I understand the API correctly, it is enough to position(pos) on a FileOutputChannel and then use transferFrom

However transferFrom always returned 0. Then I used setLength on the RandomAccessFile where the FileOutputChannel was derived from and then it worked.

This was for Java 1.6 developer preview on OS X 10.4, maybe it is not even a Problem on other systems.

Maybe you know the situation: you are developing a web application using the Maven build system along with an application server like Tomcat or Jetty and a corresponding plugin (e.g. the all famous jetty-maven-plugin).

One of the coolest features of these containers is the hot-deployment.

However, every second time a hot deployment is executed, it crashes due to a PermGen space Exception or a similar OutOfMemoryException.

Read more…

When playing around with the positions of Windows in Java, I have found a nice Java function: setLocationByPlatform of the Window class enables you to let your JVM choose the position of a new Window. This means that all windows won’t be placed at x=0 and y=0 but will be overlapped according to what your platform thinks is nice.

Recently I stumbled across a problem with the rich:modalPanel component of the JBoss RichFaces component library, which didn’t want to show the data of a backing been.

Let’s assume the following scenario:
You have a DataTable with several rows. Each row contains a “See details…” button. This button will popup a modal panel using rich:componentControl and the panel is populated with data of a backing bean. This backing bean is filled with actual data by clicking our “See details…” button using a4j:actionparam

Read more…

For p300, I am currently using a Selector to check if I can read from or write to a socket.

However, this has lead me to a weird problem: select is called for at most 30 seconds and I get a timeout for writing even though this can’t really be and only happens with some OS combinations. I’ve found this thread in the Sun Forums and adapted my internal logic to the information from this forum thread:

Instead of always selecting for OP_WRITE when I want to write, I just try to write. If I was not able to write everything (that is, buffer.remaining () is bigger than 0) I select for OP_WRITE.

This seems to work fine.

There are a lot of tips and hints out there on the internet, which describe how you could add support for Java Server Faces and Facelets to the Eclipse environment.
Mostly, they deal with adding code completion for JSF/Facelets tags and so on. There are some possibilities to achieve this, ranging from creating TLD files to using the JSP editor in Eclipse for the XHTML code of the Facelets pages.

But the “trick” I prefer is much more simple :D

You want to see it? Well, just download and install the JBoss Tools and you have it all!

Usually, when you enter standard <!-- ... --> html comments in your XHTML definition for a Facelets view, these comments are rendered by Facelets, so they are included in the resulting HTML. Even EL contained in these comments is executed!

In most cases, this is not what you hope to see, because if you want to make real comments, you have to use the <ui:remove> tag, which is not really comfortable.

Read more…

You are wondering why your JUnit tests run very fine when you launch them using Eclipse, whereas they all break down when building your project with a build system like Ant or Maven?

Well, thats probably because you are using assert statements and Eclipse per default is not configured to evaluate them.

To solve this problem, simpy go to Run -> Run… -> Arguments, and in the box labeled VM arguments:, enter either -enableassertions or just -ea.

That’s it!