Skip to content

Monthly Archives: November 2011

Java stuff


Here is some Java stuff I’ve written over the last year for Daily Fratze. All of the stuff is in use on Daily Fratze since June 2011.

java-akismet is a simple client for Akismet based on the latest version of Apache HttpComponents.
I really like the idea of oEmbed: A mechanism for auto embedding stuff from other sites so that users don’t have to paste some html code into a textbox but just plain links. This is my version of a configurable Java client that can autodetect oEmbed endpoints as well as statically configured endpoints.
This is my idea of an autolinker based on jsoup. If you want to get autolinking right, you have to parse the text. Just scanning for regex that matches urls or email addresses is not enough. This autolinker first parses the text into a DOM tree and passes all text nodes to the configured linkers. At the moment it supports URLs, email addresses and twitter handles.

I’d be happy if someone can actually use this stuff too or even contribute to it :)

Fixing hibernate “Cannot release connection” exception using DBCP and MySQL.


Every 8 hours i got a Hibernate exception “Cannot release connection” within a Java application using Hibernate, Apache DBCP on Tomcat:

org.hibernate.exception.GenericJDBCException: Cannot release connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(
    at org.hibernate.exception.SQLStateConverter.convert(
    at org.hibernate.exception.JDBCExceptionHelper.convert(
    at org.hibernate.exception.JDBCExceptionHelper.convert(
Caused by: java.sql.SQLException: Already closed.

Not only that the messages polluted my inbox, the exception was visible to the enduser, resulting in a HTTP 500 error. An older blog post i found suggested dismissing DBCP and using c3p0, a solution that i’m not quite found of. At least, the post helped to reproduce the problem within my development setup. The underlying problem was indeed the MySQL wait_timeout.

There’s quite a long documentation on the Tomcat JDBC Connection Pool. Although the Tomcat team recommends their own solution since Tomcat 7, i still wanted to go with DBCP.

The relevant keywords are “testOnBorrow”, “testOnReturn”, “testWhileIdle”, “validationQuery” and “timeBetweenEvictionRunsMillis”. The first 3 are boolean values. If set to true, the query given as validationQuery is executed on borrowing a connection from the pool, on returning or when idling. The first option is not an option on production use as the query is executed before each call. Although “Select 1″ is probably very fast, i just don’t want to have. Also: The problem is an invalidated, idle connection so i set testWhileIdle to true. And what happened? Nothing! The problem stayed. So there is the last option timeBetweenEvictionRunsMillis which should, according to the docs, default to 5 seconds but it doesn’t. The documentation is wrong. It’s under zero, so the eviction thread that tests idle connections never run. I’ve tweeted the tomcat team, but there was no reaction.

So the correct configuration for a DBCP pool database source is:

	validationQuery="Select 1"

This way the eviction thread runs every 30 minutes, testing idle connections with the query “Select 1″ and removing them from the pool. The timeBetweenEvictionRunsMillis should not be to low. It should be adapted to the configured MySQL wait_timeout.

Old and tired?


Right now i’m at the #jaxcon in Munich. While sitting in a talk, this tweet by @cuchulin caught my attention:

I really don’t think that’s Java who’s old and tired, neither as a language nor an ecosystem. It’s the people, who not only are looking tired but who are. Well, at least, I am.

I’m a curious person and always eager to learn new stuff but the last 3, 4 years the learning process and the stream of new tools, techniques, frameworks and other stuff accelerated even more than it did in the transition from client/server development to multi-tier-architectures 10 years ago.

At each conference there are new tools. New tools to “get things done”, new paradigms to organize your work and new tools to create stuff. It’s getting real hard to choose and if you want to get your head around the full stack, you’ve plenty of stuff to try, read and experiment on.

The last 2 years saw the coming of so many new languages. Some will vanish, some will stay, but it’s for sure: Another field of learning.

Given that situation plus the stream of information we get the whole day now (tweets, g+, facebook and more) it’s a tiring situation. Most of the people in the talks sit in front of their laptops or have an iPad and are constantly checking twitter and the like. Who wouldn’t be tired?

I really appreciated Adam Beans Talk “Just developed” yesterday. We really need to focus more on the things we want to create and to some extend less on the tools and methodologies.

On a personal note: I am tired. Not because of my “hard” family life with a 2-year-old, absolutely no. I just can’t really turn of my head anymore. The boundaries between work, home and spare time are diminishing. Most of the time it’s a good thing because i can spend more time at home with my family but on the other hand i have all the stuff to think about, to read and to try out often for the late evening and then they are to stay in my head.