Tuesday, October 11, 2011

A dirty secret

"The dirty secret of syndicated feeds is that XML best practices often have been flushed right down the toilet." - ROME in a Day: Parse and Publish Feeds in Java (Mark Woodman).

Tuesday, September 27, 2011

Argh!

Being a good programmer requires much patience.

Monday, September 26, 2011

How to process large data sets in Hibernate

Here's a good post discussing three ways to handle the processing of large data sets in Hibernate.

Thursday, September 22, 2011

Eclipse Mercurial plugin uses executable with version different than locally installed executable

I encountered the following issue:

1. Cloned a Mercurial repository from the command line.
2. In Eclipse, went to File -> New -> Other... -> "Create New Mercurial Repository"
3. Browsed for the newly cloned local repository and clicked the "Finish" button.
4. G0t this error in Eclipse: "abort: requirement 'dotencode' not supported!"

This was happening due to a version mismatch between the Mercurial executable used by Eclipse and the Mercurial executable installed locally. To fix this:

1. Went to Windows -> Preferences -> Team -> Mercurial in Eclipse.
2. Unchecked the "Use default (built-in) Mercurial executable" check-box.
3. Browsed for local Mercurial executable in the "Mercurial Executable" field.
4. Clicked the "OK" button on the dialog.

Thursday, April 28, 2011

Calling Postgres function with integer[] input from Hibernate

Situation: Running Hibernate, Postgres. Have function defined in Postgres taking integer[] as input. Have defined in a Hibernate mapping file which calls the function using a variable parameter ":ids". Want to call this , passing in a List. How to do it?

Answer: Create a class named HibernateArrayLongType which extends UserType. Define the two core methods in this class as follows:

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Array value = rs.getArray(names[0]);
if (value != null) {
return value.getArray();
}
else
return null;
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {

if (value == null) {
statement.setNull(index, Types.ARRAY);
} else {
List list = (List)value;
Long[] array = new Long[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
// the "integer" type name may be specific to Postgres
Array sqlArray = statement.getConnection().createArrayOf("integer", array);
statement.setArray(index, sqlArray);
}

}


In a class which extends HibernateDaoSupport, add this code:

List ids = new ArrayList();
Query query = getSession().getNamedQuery("queryName");
query.setParameter("ids", ids, Hibernate.custom(HibernateArrayLongType.class));
List result = (List)query.list();

Monday, January 10, 2011

Tip for testing database function performance

Suppose you've got a database function that's slow. You want to determine where the bottlenecks are, so you decide to pepper your function with RAISE NOTICE statements to output the current time at different points within the function. You try "RAISE NOTICE current_timestamp" and note that every time this statement is executed, the same timestamp is output. It's as if current_timestamp has been pre-set to be the moment in time when the function began executing. What to do? The solution is to use "RAISE NOTICE timeofday()" - this will reflect the actual time, not the time the function began executing.

(Please note that this applies to Postgres. I don't know if this post translates precisely for other DBMS's.)