Dependency Management Standardization

David Lloyd and I are working on getting some standards together for dependency management. We are hoping that this will help unify projects and provide the ability for all the different projects to use the same repositories.

Please join us for the discussion if you have ideas you want to share.

Here are the details:

10am PDT October 21
##java-modularity on

IE filter gradient and positioning bug

Today I found a bug in the IE rendering engine that revolves around IE positioning and using filter’s to handle gradients. My issue was that I had a navigation component that contained a drop-down menu. These drop down menus weren’t showing up in IE for some reason. After many hours of debugging, I traced it back to a gradient I was using on the navigation element using IE’s filter definition.

Here is the rough HTML:

<div class="main-nav">
    <li class="level-1">
      <a href="...">Menu 1</a>
        <li><a href="...">Sub Menu 1.1</a></li>
        <li><a href="...">Sub Menu 1.2</a></li>
    <li class="level-1">
      <a href="...">Menu 2</a>
        <li><a href="...">Sub Menu 2.1</a></li>
        <li><a href="...">Sub Menu 2.2</a></li>

The CSS that was failing looked something like this:

.main-nav {
  background: #e5e6ec; /* Old browsers */
  background: -moz-linear-gradient(top, #ffffff 0%, #e5e6ec 100%);  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fffff), color-stop(100%, #e5e6ec));  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #ffffff 0%, #e5e6ec 100%);  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #ffffff 0%, #e5e6ec 100%);  /* Opera11.10+ */
  background: -ms-linear-gradient(top, #ffffff 0%, #e5e6ec 100%); /* IE10+; */
  background: linear-gradient(top, #ffffff 0%, #e5e6ec 100%); /* W3C; */
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ffff', endColorstr = '#e5e6ec', GradientType = 0); /* IE6-9 */

.main-nav ul {
  padding: 4px 35px 2px 35px;
  vertical-align: top;

.main-nav ul li.level-1 {
  display: inline-block;
  position: relative;

.main-nav ul li.level-1 a {
  display: inline-block;
  padding: 5px 20px 5px 0;

.main-nav ul li ul {
  background: #e5e6ec;
  display: none;
  padding: 0 15px 10px 15px;
  position: absolute;
  top: 29px;

The issue with IE 8 (and probably IE 9) is that the gradient definition of:

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#ffff', endColorstr = '#e5e6ec', GradientType = 0);

caused the drop-down menus to not show up correctly. After moving them around and using some visibility magic, I found that the drop-downs were actually getting clipped inside the outer UL element. Once I removed the filter it all worked fine.

Database date/time support sucks

Okay, after many hours of battling with MySQL and PostgreSQL, I’ve come to the conclusion that databases support for dates and times suck. PostgreSQL appears to do a much better job than MySQL, but overall they both leave a lot to be desired. The main

The main issues with both these databases is that the lack the ability to store values as UTC without any timezone information. That’s right, I don’t want the database to know anything about timezones. See, once the database starts to store timezone information or worse, store dates and times without timezone information but not in UTC, you get into the situation where everything melts down if the timezone of the operating system or the database server changes. You also get into really nasty issues when you migrate data across locations, for example by exporting data from a server in San Francisco and migrating it to a server in London.

If you think about it, databases are primarily used for storing and retrieving data. In 99.9999% of applications, the database is NOT responsible for displaying data. For this reason, the database should not know anything about timezones because these are purely for display purposes. The correct way to store date and time data is at UTC.

So, what’s my solution given the horrible date and time support in databases? Don’t use them and use bigint instead. Store everything using the number of milliseconds since epoch in UTC. Never ever do any timezone math with your dates and times. Handle everything in UTC and only do timezone manipulations just before you display the data to a user.

I pulled this off by changing all of my datetime and timestamp columns to be bigint instead. They now look like this:

create table foo (
  insert_instant bigint not null,
  update_instant bigint not null

Additionally, in my application code when I use JDBC I pull the data out as Longs like this:

// using Joda here - if you don't use Joda you should
ResultSet rs = ...;
long insertInstant = rs.getLong(1);
DateTime dt = new DateTime(insertInstant);

Lastly, I wrote a Hibernate UserType that converts from longs to Joda DateTime instances and annotate my entities like this:

public class Foo {
  @Column(name = "insert_instant", updatable = false)
  @Type(type = "com.inversoft.cleanspeak.api.domain.hibernate.DateTimeType")
  public DateTime insertInstant;

And that’s it. It works perfectly and I never have to worry about any of my data getting screwed up if someone changes the system timezone or migrates data from one server to another. Furthermore, I finally get full support for milliseconds on MySQL and I also can add support to Clean Speak for new databases easily.

Database handling for TimeZones

Today I’ve been working with Postgresql and MySQL trying to figure out how they handle date-time values and timezones. This is actually quite tricky, so I wanted to write it down for later. First, both databases have two different types of columns:

  • One that stores the date-time without any time zone information
  • One that stores the date-time with time zone information

For Postgresql, these are:

  • timestamp without time zone
  • timestamp with time zone


For MySQL, these are:

  • datetime
  • timestamp

There are a number of things to consider when dealing with date-time values:

  • What if the timezone of the server changes?
  • What if the server moves physical locations thereby indicating a new time zone?
  • What if the timezone of the database server changes (different than the timezone of the server)?
  • How does the JDBC driver handle timezones?
  • How does the database handle timezones?

To figure all of this out, it is important to understand how the date-time value goes from the application, to the database, out of the database and back to the application (for inserts and later selects). Here is how this works for values without timezone information:


  1. Java creates a java.sql.Timestamp instance. This is stored as UTC
  2. Java sends this value to the JDBC driver in UTC
  3. The JDBC driver sends the value to the database in UTC
  4. The database converts the date-time from UTC to its current timezone setting
  5. The database inserts the value into the column in the current timezone (NOT UTC)


  1. The database selects the value from the column in the system timezone
  2. The database converts the value to UTC
  3. The database sends the UTC value to the JDBC driver
  4. The JDBC driver creates a new java.sql.Timestamp instance with the UTC value

For this scenario, you will run into major issues if the the server or database timezone changes between Insert #5 and Select #1. In this case, the value will not be correct. The only way to make things work correctly for this setup is to ensure no timezone settings ever change or to set all timezones to UTC for everything.

For the types that store timezone information, here is how the data is passed around:


  1. Java creates a java.sql.Timestamp instance. This is stored as UTC
  2. Java sends this value to the JDBC driver in UTC
  3. The JDBC driver sends the value to the database in UTC
  4. The database calculates the offset between the current timezone and UTC
  5. The database inserts the value as UTC into the column along with the offset it calculated


  1. The database selects the value from the column in UTC along with the offset
  2. The database sends the UTC value to the JDBC driver
  3. The JDBC driver creates a new java.sql.Timestamp instance with the UTC value

Since the JDBC driver only handles UTC values, there is no potential for data mangling here since everything is stored in UTC inside the database. Although the database stores the timezone information along with the date-time value in UTC, it isn’t ever used because the JDBC driver doesn’t care what the original timezone was and ignores that value.

Oracles OLTP numbers vs. Clean Speak

Oracle is hosting a webinar today covering the awesome performance of their Exadata system. Sure Oracle’s OLTP tests probably do slightly more than Clean Speak does, but 1,870 transactions in 2 minutes hardly seems like something to rave about.

The Clean Speak profanity filter handles between 5,000 and 10,000 messages per second. The profanity filter doesn’t hit the database, but the Clean Speak Moderator tool does.

The latest tests of the Moderation tool put it around 1,000-2,000 messages per second.

So, if we use Oracle’s time granularity of 2 minutes, Clean Speak handles anywhere from 60,000-600,000 messages. Perhaps Oracle needs to work on optimizing their OTLP test system a bit.

Escaping JSON in FreeMaker

The ?js_string doesn’t work quite right for JSON since it ends up escaping single-quotes. This isn’t quite right for JSON since it only allows Strings to be specified using double-quotes. Therefore, in order to properly escape Strings in FreeMarker for JSON, you need to write a custom method and add it to the model. Here’s my code for a JSON escaper method:

public class JSONEscape implements TemplateMethodModelEx {
  public Object exec(List arguments) throws TemplateModelException {
    if (arguments.size() != 1) {
      throw new TemplateModelException("jsonescape takes a single parameter");

    return JSONBuilder.escape(arguments.get(0).toString());

The JSONBuilder is a class that I wrote for creating JSON using the builder pattern. However, the escape method looks like this:

  public static String escape(String str) {
    StringBuilder build = new StringBuilder();
    escape(str, build);
    return build.toString();

  public static void escape(String str, StringBuilder build) {
    char[] ca = str.toCharArray();
    for (char c : ca) {
      switch (c) {
        case '"':
        case '\':
        case 'r':
        case 'n':
        case 't':
        case 'b':
        case 'f':
        case '/':
          if (c >> 12]);
    build.append(HEX_CHARS[(ch >>> 8) & 0xf]);
    build.append(HEX_CHARS[(ch >>> 4) & 0xf]);
    build.append(HEX_CHARS[ch & 0xf]);

Then I add this method to my model and render the template:

model.put("jsonescape", new JSONEscape());

My templates look like this:

  "foo": "${jsonescape(}"

Friendster can't even send email

I’m surprised that Friendster is even still around, but today I got an email from their CEO and it had the entire MIME body in the message. These guys can’t even figure out how to send email.

X-Campaign-ID: 8002
X-Campaign-Type: 1
X-Message-Ref: 19lGRwmaJkSjEf8FZP7XSCijJOJhOejGu832ySW13bA*
Content-Type: multipart/alternative; boundary="----_=_FSter_001_3251309519900466"
MIME-Version: 1.0

This is a MIME email, the first part is the text message.

Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Friendster July 2011

A Personal Message from Friendster's CEO

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Dear fellow Friendster members,
As many of you may know, Friendster announced that it is re-launching itself as a social gaming portal and launched a beta version of the new Friendster a couple of weeks ago. The beta version was well received. I am pleased to announce that the new Friendster is going live thereby enabling all our users to login to the new Friendster using your existing Friendster username and password.
Friendster has touched the lives of many. Since MOL, the company I founded acquired Friendster in early last year; many people have come up to me to tell me how Friendster has changed their lives. Many have told me that they have found their life partners over Friendster. Just last week, a successful Internet entrepreneur in Singapore told me that her success was triggered by promoting her business on Friendster. Friendster pioneered social networking and ignited the social media industry that has created billion dollar companies such as Facebook and Twitter, companies that may not have existed in their present form if not for Friendster's early innovation.
Today, Friendster is in a unique position to take advantage on the growth of social gaming. Through its relationship with MOL, which has a 10 year history in working with gaming companies, Friendster has both the experience and track record to make innovations in this space.
Today, as Friendster reinvents itself as a social gaming destination that enables its users to create multiple avatars, play games and enjoy rewards; I hope that all of you will wish us luck and continue to support us in our new reincarnation. The new Friendster is not perfect and we will continue to add new games and features such as localization and rewards over the next few months. Our team is working hard on adding these features and welcomes your suggestions and comments on how we can better serve your needs as a social gaming and entertainment destination.
I would like to take this opportunity to thank all of you for your support and hope that all of you will enjoy the new Friendster as Friendster continues to innovate to serve and entertain you better.
Yours truly,
Ganesh Kumar Bangah
Chief Executive Officer

To manage your notification preferences go to:
Copyright 2002-2011 Friendster, Inc. All rights reserved.

Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

[SNIP - I cut out the HTML so I didn't have to escape everything by hand]



JDBC Batch vs. Multi-Row Inserts

I recently had a requirement to insert a few hudred rows into a relational database ever couple of seconds. Generally this could be accomplished during the request, but I didn’t want to introduce issues if there were spikes. Therefore, I figured I would cache the data and then write it out in a background thread every few seconds. This would also increase my response time during requests.

I wasn’t sure whether or not JDBC batch or using a single insert statement that inserted multiple rows would be faster. Therefore, I setup a little test to see which was going to work better. First some code:

Here is the code for a single insert statement that inserts multiple rows.

long start = System.currentTimeMillis();
StringBuilder build = new StringBuilder("insert into insert_values (foo, bar, baz) values ");
for (int i = 0; i < 1000; i++) {
  build.append("(?, ?, ?)");
  if (i < 999) {
    build.append(", ");
  } else {

Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch_vs_insert_test", "dev", "dev");
PreparedStatement ps = c.prepareStatement(build.toString());
for (int i = 0; i < 3000; i++) {
  ps.setString(i + 1, "value" + i);

int result = ps.executeUpdate();

long end = System.currentTimeMillis();
System.out.println("Insert time was " + (end - start));


Here is the code for the JDBC batch:

long start = System.currentTimeMillis();
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/batch_vs_insert_test", "dev", "dev");
PreparedStatement ps = c.prepareStatement("insert into insert_values (foo, bar, baz) values (?, ?, ?);");
for (int i = 0; i < 3000; i++) {
  ps.setString((i % 3) + 1, "value" + i);
  if ((i + 1) % 3 == 0) {

int[] results = ps.executeBatch();

long end = System.currentTimeMillis();
System.out.println("Batch time was " + (end - start));


The average time over 10 iterations where roughly as follows:

JDBC Batch: ~100 milliseconds
Single Insert: ~10 milliseconds

It looks like the single insert statement wins by a large margin.

UPDATE: This method yields very similar results on both MySQL and PostgreSQL using the latest drivers from each organization.

iMac vs. build-your-own

I’ve been meaning to do this comparison for a while now, but this morning I decided to just jump in and do it. A lot of people are constantly complaining about how expensive Macs are and I’ve long held the notion that they are quite reasonably priced.

Here’s my break down:

  • CPU (i7 2600K 3.4GHz Sandybridge) $320
  • Motherboard (ASUS P8P67) $185
  • RAM (8GB DDR3 1333MHz) $83
  • Hard Drive (WD 1TB 7,200 RPM SATA 6G) $85
  • Video (ATI Radeon 6970M) $300
  • Keyboard/Mouse (MS wireless) $80
  • Monitor (Dell Ultrasharp 27″ 2560×1440) $1000
  • Webcam (Any) $50
  • Speakers (Any) $15

The main expense here is definitely the CPU and the monitor. However, for equal comparisons I wanted to add a monitor that was comparable in quality, display, depth, etc. You could obviously go with a cheaper monitor from an off-brand, but I think that’s not a fair comparison.

Here’s the results:

  • Apple iMac 27″ with 3.4 GHz CPU and 8GB RAM factory installed: $2,199
  • BYO with above specs: $2,118

The difference is about $81.

I poked around a bit at some desktop solutions as well, and even an entry level desktop with the i7 2600K and the Dell ultrasharp monitor is going to start around $2,000.

It looks like my notion about the value of a Mac is accurate. Furthermore, with the fact that the iMac is a single piece of equipment that includes the computer and the monitor and you get what I still feel is the worlds best operating system plus a bunch of free software, it is hard to argue that an iMac isn’t a great value.

Java Road Trip – Where have all our heroes gone?

I got an email from Oracle about the Java Road Trip. Looking over the website I wasn’t immediately excited to attend the event here in Denver. But even more than that, I looked at the profiles for the speakers and the staff and all I can say is, “Where have all our heroes gone?”

I didn’t recognize a single name. Maybe I just don’t get out much anymore. Or maybe Sun did a piss poor job of promoting their employees and rock-stars. Or maybe they shackled them to their desks or something. Who knows?

Hopefully Oracle and fix some of that and help me recognize the name “Tony Printezis” and be excited that he’s working on the JVM.