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
java-akismet is a simple client for Akismet based on the latest version of Apache HttpComponents.
java-oembed
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.
java-autolinker
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 🙂

| Comments (1) »

28-Nov-11


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(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    ..
    ..
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:

<Resource
	type="javax.sql.DataSource"
	driverClassName="com.mysql.jdbc.Driver"
	maxActive="100"
	maxIdle="30"
	maxWait="10000"
	testOnBorrow="false"
	testOnReturn="false"
	testWhileIdle="true"
	validationQuery="Select 1"
	timeBetweenEvictionRunsMillis="1800000"
/>

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.

| Comments (2) »

21-Nov-11


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.

| Comments (4) »

09-Nov-11



Java and invalid SSL certificates (java-trustprovideragent)

It’s truly easy to generate a SSL certificate for example to use with tomcat (see here). This certificate is invalid as it is self-signed by you and it often doesn’t match the hostname. This is no problem when your access the project with a browser, with more or less jumps through hoops you accept the development certificate and you’re done.

If you access the site through java itself you’ll have problem with all tools that basically use an URLConnection. You’ll end up with an exception like this:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target

This is will hit you for example using HtmlUnit or my oembed client.

It isn’t enough to import the certificate in question using keytool (at least, it didn’t work for me).

I search and i found this post titled “SSL Trust Provider for Java”. Interesting stuff.

This works by providing a “java.security.Provider” through the Security API accepting all certificates. Nice tip, thanks!

I didn’t want to change my sources though so i wrote a very little java agent to instrument my development setup. I also added a “javax.net.ssl.HostnameVerifier” that accepts all host names, in case the certificates cn doesn’t match the development machines hostname. If i want my vm to trust all and everything, i just add “-javaagent:full/path/to/java-trustprovideragent-0.0.1-SNAPSHOT.jar”.

The code is on github java-trustprovideragent, please feel free to use it.

Thanks to the original authors on devcentral.f5.com.

| Comments (0) »

25-Jul-11