JDBC Connection Pool and MySQL

September 7th, 2007

The web application I created is running on Apache Tomcat 6.0.14 with MySQL database. I decided to setup a data source in ${tomcat-home}/conf/Catalina/www.somedomain.com/ROOT.xml. The XML file looked like:

<context docbase="/wars/application.war">
    <resource>
        name="jdbc/ApplicationDS"
        auth="Container"
        type="javax.sql.DataSource"
        username="db_user"
        password="db_password"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/db"/>
    </resource>
</context>

Unfortunately the connections to the server closed after 8 hours of inactivity with the following exception.

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException:
    No operations allowed after connection closed.
    Connection was implicitly closed due to underlying
    exception/error:

** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to
    underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Broken pipe

STACKTRACE:

java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(...:92)
    at java.net.SocketOutputStream.write(...:136)
    ...

I googled about the problem with JDBC connection pools and MySQL, and found a lot of information how to fix it. Most of the hints were to add autoReconnect=true to the DB connection URL (e.g. url=”jdbc:mysql://localhost:3306/db?autoReconnect=true”). But I read, that this may cause problems and will be removed in later versions of the JDBC driver.

The correct usage and configuration of the JDBC connection pool for MySQL can be found MySQL Reference Manual: Using Connector/J with J2EE. I changed my Tomcat configuration to the XML below, and since then I had no problems anymore:

<context docbase="/wars/application.war">
    <resource>
        name="jdbc/ApplicationDS"
        auth="Container"
        type="javax.sql.DataSource"
        
        maxActive=”50″
        maxIdle=”10″
        validationQuery=”SELECT 1″
        testOnBorrow=”true”
        testWhileIdle=”true”
        timeBetweenEvictionRunsMillis=”10000″
        minEvictableIdleTimeMillis=”60000″
        
        username=”db_user”
        password=”db_password”
        driverClassName=”com.mysql.jdbc.Driver”
        url=”jdbc:mysql://localhost:3306/db”/>
    </resource>
</context>

jsp-config in web.xml

July 23rd, 2007

I’m using Apache Tomcat 6.0.13 which implements Servlet 2.5 and JSP 2.1 specification. As I outlined earlier I’m using Stripes web application framework and JSPs and JSP tag files for rendering the page. With this combination I’m quite satisfied, because it enables me quick and efficient development.

Additionally I discovered the <jsp-config> configuration possibilities in the web.xml file. There you can configure the behavior of a group of JSP files. For instance I use the <page-encoding> and <trim-directive-whitespaces> configuration for all JSP files.

Page encoding I set to “UTF-8″ as default. In general it is a good suggestion to use UTF-8 from the beginning on with your web projects on any layer (database, application server, front-end). Because you will have less problems with character sets and special characters when it comes to internationalization and localization.

Trim white spaces is new in the JSP 2.1 specification, and very useful. When set to true, it removes all empty lines in the output before returning it to the browser. This makes your HTML more readable. Unfortunately it does not remove the empty lines in the JSP tag files :-(

So my web.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <servlet>
        ...
    </servlet>

    <servlet-mapping>
        ...
    </servlet-mapping>

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <trim-directive-whitespaces>
                true
            </trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>
</web-app>

QuickTime Update Breaks PowerPC Applications on Intel Macs

July 19th, 2007

Last week I installed the update from Apple that installed QuickTime 7.2 and iTunes 7.3.1. After that some of my PowerPC based applications (e.g. Microsoft Office, Photoshop CS2, Dreamweaver 8 ) did not start anymore. I clicked the programs on the dock, they flickered 2 or 3 times and nothing happened. The system.log in the console showed me the following error message:

Jul 19 08:24:35 markus-tripps-computer kernel[0]:
    shared_region: 0x40d1cec:
    lsf_map: RO mapping #0 not in segmentshared region:
    0x40d1cec: 4 mappings  base_offset=0xff0b000
Jul 19 08:24:35 markus-tripps-computer kernel[0]:
   shared region: 0x40d1cec:
   #0: addr=0x70000000, size=0x17000,
   file_offset=0x2d000, prot=(5,5)
...

It took me a while to find the problem. In my case it seams to be a combination of

  • QuickTime 7.2 update
  • Installed Java SE 6.0 Release 1 DP6
  • PowerPC based application like Dreamweaver 8

that caused the problem.

The solution to this problem I found at http://discussions.apple.com/thread.jspa?threadID=1039074&start=135&tstart=0.

Short description what I did:

  • De-install JavaSE6Release1.pkg
  • De-install JavaForMacOSX10.4Release5.pkg
  • Run update_prebinding as root user
  • Install JavaForMacOSX10.4Release5.pkg
  • Install JavaSE6Release1.pkg

At the Apple support page “bigman13″ (who posted the solution there) describes the steps as follows:

Steps to Fix:
> su Administrator
> cd /Library/Receipts/JavaSE6Release1.pkg/Contents
> lsbom -s -f Archive.bom > /tmp/files

Edit /tmp/files replace all " " with " "
   and all "./" with "rm /"
> sudo bash /tmp/files
> rm -r /Library/receipts/JavaSE6Release1.pkg
> sudo update_prebinding

I removed /Library/Receipts/JavaForMacOSX10.4Release5.pkg
and installed it again.
(To have the links from A, Current etc.)

On the one hand it is quite annoying to search for such problems, and it took me some hours, on the other hand it helps me to understand MacOSX a bit better, as I use it only for 2 weeks now ;-)

Stripes and JSP tag files

July 19th, 2007

My current project is to develop a backend system in Java, with a remote interface and a normal web application. At the moment I’m writing the web application.

Before I started I had to decide if I use a web application framework, or just use standard JSP and write my own dispatcher Servlet. In the last couple of years I worked with so many different technologies like Struts, WebWork, JSF, Bea WebLogic Portal, but none of them satisfied my completely. The best of them was WebWork.

But then I got the hint from a friend to try Stripes. I had a look at the quick start guide, and was pretty amazed. Everything looked so easy, and best of all, so little configuration is required. Stripes uses Java Annotations and conventions quite intelligent to configure and manage the web application. To get a feeling how easy it is, here a short example. Think about a web page that contains a form, where you can enter your email address to receive newsletters.

All you have to do is to write a class that extends ActionBean and forward to a JSP. ActionBeans are similar to Action classes in Struts, but with some usefull extensions. For instance parameters are automatically populated by Stripes, and validation of parameters can be configured through Annotation.

Example: NewsletterActionBean.java

package com.company.project.action;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.action.ActionBeanContext;

public class NewsletterActionBean extends ActionBean {

    @Validate(required=true) private String email;

    public Resolution subscribe() {
        NewsletterService.subscribe(email);
        return new ForwardResolution("/newsletter.jsp");
    }

    // Bean getters and setters

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // Usually moved to a BaseActionBean class

    private ActionBeanContext context;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
}

That’s all. The email is injected and validated by the Stripes framework, the subscribe method stores the email in the NewsletterService, and forwards to the newsletter.jsp page for rendering the HTML.

The next technology I have to mention are JSP tag files. Introduced in the JSP 2.0 specification it provides an alternative mechanism to write tag libs. I don’t know why this is not mentioned more often, because it’s really a cool extension and I use it quite heavily. You don’t have to write a Java class file. The only thing that is required, is a tag file (*.tag) that is placed in WEB-INF/tags (or META-INF/tags or in a JAR in WEB-INF/lib), and use it in the JSP file.

Example: index.jsp

...
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
...
<html>
<head><title>Some Title</title></head>
<body>
    <ui:header id="header">
        <a href="http://www.company.com">
            <img src="logo.png" />
        </a>
    </ui:header>
....
</body>
</html>

Example: header.tag

<%@ tag language="java" %>
<%@ attribute name="id" required="true" %>
<div id="${id}">
    <jsp:dobody/>
    <div id="headerMenu">
        <ul> ... </ul>
    </div>
</div>

Is pretty simple ;-)

With this tag files you can do quite a lot. One possibility is to create GUI components for your project. E.g. if you have a message dialog with rounded corners that has to be placed on many places, then you can create a tag called: message.tag and use it in the JSP.

Example: message.tag

...
<ui:message>
    Place message dialog text here.
</ui:message>
...

My New MacBook Pro

July 4th, 2007

I ordered my new MacBook Pro mid of June, and yesterday it finally arrived. It’s a 17-inch model, 2.4 GHz Core 2 Duo, 2 GB RAM, 160 GB hard drive @ 7200 rpm, HiRes display (1920 x 1200).

Until now I was mainly using Windows based desktops or notebooks. But especially for software development many of my friends and partners switched to MacBooks. After 1 1/2 days I will say, it was a good choice. Let’s see what the future brings, but you will find some of my experiences and hints with the new Apple notebook on this Webog.

As a start I have the following applications installed:

  • Firefox: I prefer Firefox 2 over Safari, and use Flock 0.9 beta for testing, and blogging. For my daily work I’m very happy with Firefox, and I use some add-ons like del.icio.us, Gmail Notifier, Feed Sidebar, Fission, …
  • Java 5 and 6 with the new Eclipse Europa release for Java Enterprise Development. Eclipse provides me with everything I need for development. I was a little bit disappointed that Java 6 is only available as developer preview, but until now I had no problems with it.
  • Skype: Great software, I love it.
  • NeoOffice: Instead of OpenOffice I try NeoOffice which is also based on OpenOffice but has native Aqua support, and does not require X11. Let’s see how it works.

Basically that’s all I need in the beginning. For email, contacts and calendar I continue to use Google Apps. I use it now for a while, and it’s really good. I don’t miss Outlook or any other email application.

The only application I’m still missing is a good text editor like UltraEdit on Windows. Let’s if I find one.

Flock browser with WordPress and Flickr

June 20th, 2007

I’m using WordPress as Weblog software, and store my photos at Flickr. A few weeks ago I discovered Flock web browser, and today I gave it a try. Quite impressing!

Flock is build on top of the Gecko layout engine like Firefox and Safari, and provides some custom integrated interfaces.

  • Access to the photo services Flickr and Photobucket
  • Access to several Weblog services like WordPress or Blogger

Also the normal browser is very nice and definitely a good alternative.

Adding a blog entry is really very easy. Once setup the accounts to your services, then just click “Create a blog post” button in the toolbar, and a pop up is displayed. Here you just enter your blog text as with any other WYSIWYG editor.

Cool is the toolbar that shows the photo albums on Flickr. If you want to include a photo just drag and drop it to the editor pop up. That’s it (see screenshot below).

Flock in action

Try it out. I’m continue using it.

Sold my car

June 20th, 2007

When I returned from my work in Berlin in 2002, I bought this car, an Audi A4 Avant Quattro.

I drove it for 5 years, and I liked it very much. But now the time has come to sell it. At the moment I’m searching for a replacement, but I did not decide yet. Maybe an Italian car this time. I will see.

My Weblog is live

May 20th, 2007

It’s Sunday evening, so the right time to start my Weblog. I decided to use Wordpress hosted on my dedicated server at Vollmar. I start with the default Wordpress theme, but will create my own themes.

It’s now 3 weeks ago that I quit my job at Sony NetServices. After one week in Rome I start to get organized. I wanted to use Google Apps for my standard office tools. It offers email, calendar, docs and spreadsheet under my own domain. I’m really impressed by the Google mail web client. But docs and spreadsheet applications don’t offer enough functionality to be really a substitution for Microsoft Office or Open Office. So I just use email and calendar from Google.

Read more about me.