2 coding tools to look at

December 12th, 2012

A couple of tools to look at is Sonar ( http://www.sonarsource.org/ ) and Open Grok ( http://hub.opensolaris.org/bin/view/Project+opengrok/ )

Being bitten by SimpleDateFormat

November 7th, 2012

It’s been a while since I’ve been bitten by a multi-threading issues with the code that we produced, but this one is probably very common problem that is hidden until it’s too late. When using SimpleDateFormat, one needs to remember that it is not thread safe class: so if you have an instance of it that is shared across threads (for example static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");, you need to synchronize access to it. There are several ways to do it. The easiest is to use “synchronized” when accessing the methods for parsing and formatting, but you might pay a hefty performance penalty for doing that. If you are only interested in formatting, you can use FastDateFormat.html class. Or you can create a ThreadLocal, and initialize your format in initialValue method.

iBartNow – BART application for BlackBerry released

May 5th, 2012

check out my new application iBartNow, available on http://www.ibartnow.com. This version (0.5.0) has real time ETA and a BART map – bare essentials, but it is fast and usefull for an everyday BART commuter. Enjoy!

Throwing the Elephant – zen and art of managing up

December 31st, 2010

My boss suggested I read a book or two on managing up, so I chose a few books that scored high on Amazon and were available at my local library branch. One of them was: Throwing the Elephant – zen and art of managing up.

The book is funny, but I did not find it too useful, since it mainly deals with Executive Level management. Here are quick points I remember from the book. It starts with the zen idea that nothing matters that much – we all are part of corporation, there is no self, there is no boss. Sit up, breathe, drop your ego, cheer up – remember nothing matters.

If you are willing and able to work, it gives you incredible power – since elephant does not want to return to do it. When meeting the creature look it in the eyes, look your best, and say minimal hello unless you know a lot about it already – e.g. it behooves you to study it before meeting. Language is self – learn the language! Know it’s likes and dislikes and feed it e-mails starting early in the morning to late at night. Compliment the creature. Smile.

Disobeying the elephant, which means wrong obeying, pestering disputation, non-obeying with delays. Mention, suggest to it about other people feeling, do not tell, imply. Praise it for humane actions. Convince the elephant the idea was of it’s own making. Take credit for things well done. Learn how to lead the elephant (ideas) and throw the elephant 🙂

Couple of interesting articles from Joel on Software

December 17th, 2010

Saving pointers for myself:

http://www.joelonsoftware.com/articles/fog0000000073.html – about hiring
http://www.joelonsoftware.com/articles/fog0000000043.html – about writing better code

Preparation to SCEA 5 Part II Notes

December 8th, 2010

Some notes and links that I found useful preparing for SCEA http://docs.sun.com/app/docs/doc/819-2326/gaxqg?a=view

Spring reference documentation

October 19th, 2010

In a recent project I had to remember Spring MVC, so I found the reference documentation from spring and thought it’s a good thing to post: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/pdf/spring-framework-reference.pdf.

For spring MVC 3.0 controllers try the following url http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

How to process queries that have lists/ arrays as parameters

October 12th, 2010

There were multiple times in recent projects where I had to write SQL for queries that had lists or arrays as parameters. There are a few ways of dealing with this issue, since API do not allow this natively.

One way is to use temporary table, insert all the ids in it and then join to that table in the query. Sybase has ‘native’ support for such temp tables, as long as you are using the same connection to insert the ids, and then execute the query.
To get the single connection out of the pool,use the following code:
JdbcTemplate scdsJdbcTemplate = null;
final String tempTableName = “#assets”;
try {
scds = new SingleConnectionDataSource(getJdbcTemplate().getDataSource().getConnection(), true);
scdsJdbcTemplate = new JdbcTemplate(scds);

createTempTableForIds(ids, tempTableName, scdsJdbcTemplate);

To insert ids into temp table and drop it after it was used, use the following code:
protected void createTempTableForIds(final List<String> ids, String tempTableName, JdbcTemplate jdbcTemplate) throws SQLException {
if (!tempTableName.startsWith(“#”)) {
throw new RuntimeException(“Temp table ” + tempTableName + ” does not start with #, which is a” +
“requirement for a temp table”);
}

final String TABLE_CREATE = “create table #_TABLE_NAME_ (barraId varchar(32))”;
final String TABLE_POPULATE = “insert into #_TABLE_NAME_ (barraId) values(?)”;

jdbcTemplate.execute(“sp_dboption asgp_db, ‘ddl in tran’, true”);
jdbcTemplate.execute(TEMP_TABLE_CREATE.replaceFirst(“#_TABLE_NAME_”, tempTableName));

String insertSql = TABLE_POPULATE.replaceFirst(“#_TABLE_NAME_”, tempTableName);

doInsertValuesIntoTempable(ids, jdbcTemplate, insertSql);
}

protected void doInsertValuesIntoTempable(final List<String> ids, JdbcTemplate jdbcTemplate, String insertSql) throws SQLException {
BatchPreparedStatementSetter batchObject = new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, ids.get(i));
}

public int getBatchSize() {
return ids.size();
}
};

int[] updateCounts = jdbcTemplate.batchUpdate(insertSql, batchObject);

/* sanity checking results */
if (updateCounts.length != barraIdAssets.size()) {
throw new SQLException(“Not all Ids were inserted # of inserts ” +
updateCounts.length + ” vs. # of Ids needed to be inserted ” + barraIdAssets.size());
}
for(int i=0; i< updateCounts.length; ++i) {
if (updateCounts[i] != 1) {
throw new SQLException(“Could not insert id” + ids.get(i)
+ ” inserted ” + updateCounts[i] + ” instead of 1 row”);
}
}
}

public static void dropTempTable(String tempTableName, JdbcTemplate jdbcTemplate) {
if (!tempTableName.startsWith(“#”)) {
throw new RuntimeException(“Temp table ” + tempTableName + ” does not start with #, which is a” +
“requirement for a temp table”);
}
String TABLE_DROP = “drop table #_TABLE_NAME_ “;
jdbcTemplate.execute(TABLE_DROP.replaceFirst(“#_TABLE_NAME_”, tempTableName));
}

The second way, you can dynamically create an in-clause and populate it with the ids, where you will have to make sure to batch #, e.g. possibly create a few queries since each one would work on subset of ids. You might be able then fire these queries in parallel and get the results that way.

Passed Java Certified Architect Exam part I

June 25th, 2010

I passed Java Certified Architect Exam part I – here are my preparation notes.

Problem with Sybase performance

February 10th, 2010

We had a strange problem with Sybase performance: the query that should have used indexes while executing did not, and as a result took forever to execute.  For some reason the time stamp was not treated as date type when set from the code. So the easiest solution was to add ?DYNAMIC_PREPARE=true to the connection string, and that solved the problem.