Thursday, August 12, 2010

Do you use or consider subversion as source code repository?

Check this out in case you use branches:

Unless you use it only for checking in and out files, probably subversion is not the more robust solution.

(Update: CollabNet seems to have deployed a different blog and broke existing links. Thanks to Paulo Naddeo for pointing that out.)

Thursday, June 24, 2010

Synchronized Map in Oracle driver?

I posted this thread on Oracle's forum after finding out that the top monitor in my application was a "SynchronizedMap" in Oracle driver. It seems that each prepared statement needs to access this map, getting worse as one increases the number of database operations in the system.

StringBuilder memory allocation

Today I was trying to understand a heap dump for a String that, when saved to a file, had 3 Mb in size. However, its retained dump was over 9 Mb! First, this String was a ThreadLocal variable - a library that we use to marshal this String into XML uses ThreadLocal (a good post on StringBuilders and ThreadLocal here).

A colleague tried to understand why it was 9Mb and found out a couple of things that we were not considering:
  • UTF16: each character consumes 2 bytes in a String to be represented. So, our 3Mb file would consume a minimum of 6Mb of memory space
  • StringBuilder allocation: whenever StringBuilder needs to expand its capacity, it doubles the current allocated memory. So, if we had 4.5Mb and needed one byte more, it would allocate an internal buffer of 9Mb. In our case, it was clear that 3 out of 9Mb had never been used for anything

Wednesday, June 09, 2010

Apache Commons DBCP and initial pool size

Unless you are using BasicDataSource, DBCP does not provide an initial pool size, which is very important if you want to avoid pool expansion out of sudden. It is particularly important if you are migrating from C3P0, like what I did recently. So, in this case, you will have to code an initializer to do it for you. In my case, I wrote a static method that is invoked through Spring:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool.ObjectPool;

public class DataSourceInitializer {
    private static final Log log = LogFactory.getLog(DataSourceInitializer.class);

    public static void init(ObjectPool pool, int initialSize, String dataSourceName) {
        int count = 0;"Initializing connections for data source: " + dataSourceName);

        for (int i = 0; i < initialSize; i++) {
            try {
            catch (Exception e) {
                log.warn("Failure to initialize connection.", e);
        }"Initialized " + count + " out of " + initialSize
                + " connections for data source: " + dataSourceName);
And, in Spring, I call it like this:
<bean id="dataSourceInitializer"
       <property name="staticMethod"
           value="com.example.DataSourceInitializer.init" />
       <property name="arguments">
               <ref bean="objectPool" />
               <ref bean="databasePoolSize" />

Tuesday, June 01, 2010

How to bind to all local addresses

Whenever we bind to an address, typically we bind to an address and port.
InetSocketAddress local = new InetSocketAddress("localhost", port);
This binds your socket only to "localhost". What if your host has multiple names, such as "localhost" and "myhost.mydomain"? You can still bind to all local addresses by doing the following:
InetSocketAddress local = new InetSocketAddress(port);
Internally, this binds to all localhost addresses by using InetAddress.anyLocalAddress() (which is package private).

Java Thread States

We had a little bit of debate last week about Java Thread states and I found the explanation for our questions in the Thread.State javadoc. The main question is the difference between BLOCKED and WAITING, but I guess this subtle difference is clear below:

Thread state for a thread which has not yet started.

Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.

Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

* Object.wait with no timeout
* Thread.join with no timeout
* LockSupport.park

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:
* Thread.sleep
* Object.wait with timeout
* Thread.join with timeout
* LockSupport.parkNanos
* LockSupport.parkUntil

Thread state for a terminated thread. The thread has completed execution.

Saturday, May 29, 2010

Book Review: iReport 3.7

Packt Publishing contacted me a few weeks ago and sent me a copy of their new iReport 3.7 book, by Shamsuddin Ahammad. Although it took me longer to manage to find the time to read the entire book, I got done this weekend and would like to share my thoughts about it.

iReport 3.7
What is this book about?

This book is about iReport, a very neat tool to design reports intended to run using Jasper Reports library. Although Jasper Reports is probably the best open source report library, it is very hard to use it unless you have a tool to design your reports. I had the opportunity of using iReport a few years back, and without it, I would not have used Jasper Reports at all. Although Jasper Reports has always been very powerful, I would not have written raw XML to design my reports, so now you may understand why iReport turns out to be relevant and worthy of its own book.

In addition to that, not only Jasper Reports is very powerful, but iReport is as well. I had some troubles a few years back for some more advanced tasks (like being able to use scriptlets for my reports), but even then I was able to figure out how to make it work. Recent versions are years-light better than at the time I used, so I highly recommend it as my open-source option for reports.

This book targets those who want to quickly learn iReports. It is a hands-on book, introducing the basic and necessary concepts to get started, following a tutorial format. Each chapter covers a specific topic that will get the user to quickly learn a new feature and leverage them for their own projects.

What does it cover?

As mentioned before, it covers a specific feature in each of its chapters. These are the covered topics: Report Layout and Formatting, Variables, Parameters, Groups, Subreports, Crosstab Reports, Charting, Images. It also covers how to display reports in Java applications.


This book is definitely relevant given the importance of iReport and Jasper Reports in the open source world. It doesn't require any prior knowledge of Jasper, iReport, and only very minimal knowledge of SQL to be able to learn what iReport is about. I think the tutorial format is very interesting and does not bore you even if you read it cover-to-cover, like I did. Also, if you prefer to learn just what matters to you, it leads you to the basic knowledge of iReport very quickly, what is very important nowadays. This is definitely the kind of book I would recommend to anyone that doesn't know anything about iReport.

On the other hand, this is not the only source of information you will need when using JasperReports. The integration of Jasper Reports with your applications is covered primarily through a Swing application and the integration with a web application is very short and doesn't provide details. If I were to review this book before it was published, I would have suggested that the author spent more time on the integration part. I would also remove or shorten the last chapter on Netbeans, as I don't think it's so relevant and does not provide much new information. Given that, if you buy this book, very likely you will need to look up on the web more information on how to actually generate the report files.

Also, if you are using more advanced features, they are not covered in this book. One example is scriptlets. Also, it touches on the concepts only lightly, so if you are expecting to learn theory/concepts, internals of iReport or JasperReports, or even details about iReport (like configuration options, all the properties, all available report elements), this is not the book for you.

In terms of the tutorial format, it provides the database schema and how to create in the appendices, which is definitely handy for those not familiar with MySQL. In my case, it was very simple to create the schemas copying the SQL statements from the ebook version. However, it does not provide data and it would have so simple to have provided some sample data so users do not have to populate the database to be able to follow each chapter's instructions. If you buy the print version, the code is not still available on Packt's website, so you will have to create the schema copying the SQL statements by hand. Being nit-picky, the only Java code that is provided in the book is badly indented/formatted and reviewers may have overlooked that.

Finally, I have to say that I really enjoyed reading this book and learning more about the new version of iReport. I recommend this book to all of you that would like to start learning about iReport 3.7. Please leave your comments below if you had the chance to read this book as well.