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.

Well, currently I’m working on my project filesio, a platform for distributing large files via web interface instead of FTP.

One of the core abilities of my application is to provide widgets for third-party sites, allowing my users to include download-links to their files in their own web pages. These widgets are simple iframes that load content from my domain.

However, when implementing my app I struggled with the problem that Microsoft Internet Explorer per default blocks cookies set from within an iframe (of course, all other browsers don’t do this).

Lucklily, I found a really ..hm…interesting solution for this problem:

Read more…

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!

For those of you who want to display a progress panel or a progress bar whenever some loading on your page is required, the JBoss Wiki contains a really good tip: RichFacesPleaseWaitBox.

This sample can be enhanced very easily to bring in some dynamic: within the rich:modalPanel, just use an animated gif, for instance from this cool page.

The following has been originally posted here and here on June 13th and 14th, 2005.

There are two things I want to say about this shot.

Read more…

There are many scenarios where a web application must serve a file instead of a response containing HTML output to be rendered by the browser.

This little tip illustrates how to cause the browser to display a file download dialog instead of trying to display the data directly (which fails in most cases, especially with binary data).

As you will see, the whole magic concentrates on a special HTTP header, so this tip can be used with any technology and framework, ranging from Java Server Faces (JSF) and the Seam framework to PHP and even ASP or ASP.NET.

A common way to realize file download is to simply write the file data into the HTTP response.

However, if you have to handle arbitrary files, you are not able to set a specific Content-Type header like text/plain or something else.

So the client (namely the browser) has no idea what kind of data it actually receives and what to do with it.

Here is where the HTTP header Content-Disposition comes into play. Setting this header tells the client that an attachment is received and therefore causes the browser to display a file download dialog.
Now it’s the user’s responsibility to save the file and to open it with a corresponding application.

The Content-Disposition header consists of two values: the string “attachment” indicating an attachment and a “filename” property specifying a filename for the attachment. Usually, the browser takes this value as proposed filename in the download dialog.

The following line is an example extract from an HTTP response containing a Content-Disposition header:

Content-Disposition: attachment; filename="myFile.exe"

The following might be totally obvious, but to me it was a revelation, so why not publish it. :-)

There are situations when you have methods returning a collection.
A Java collection can be a set (no sorting, no duplicates), a list (sorting, duplicates) or a map (association key/value).

In some cases, the method might fail.
To have robust code, you would still like to return an empty set, list or collection instead of null.

Read more…

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.