Sunday, April 20, 2014

Create an iphone application using xcode that dynamically creates sqlite tables

This tutorial will try to create an iphone application that will dynamically create tables in sqlite database. This tutorial will use anime information for example.

Note that this tutorial is not for beginner, I'll just summarized the steps and provide the github repository for the project.

1.) You should look at the code in my todo list tutorial before this one.
2.) Create an anime sqlite database using your preferred tool, I've used SQLite Manager plugin for firefox.

1.) Create a new empty ios project.

2.) Select the project, and in the general tab under Linked Frameworks, add libsqlite3.0.dylib.

3.) Add the sqlite database we've created, right click on the project folder click Add Files..., then select the anime.sqlite file and check "Copy items into destination...". Now we have the database inside our project.
Your project should look like:

4.) Create the main story board inside the SqliteAnimeDb folder.

5.) In the project setting set the main interface to the story board we've created.

6.) Comment these 3 lines in the AppDelegate class:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

7.) I'll let the code explain but I'm attaching the 2 views I've created for demo.

In this view we display the list of anime titles with author subtitle. Clear and reload do the functions respectively on the database. + will navigate to the next view.

This view is where we add an anime object.

KBSSqliteConnection, is the class where we put the sqlite database operations like create table, open db, insert record, etc.

Github repository:

My Todo List iphone application development using xcode

This tutorial will create a Todo list iphone application using xcode. This is the same tutorial in the apple's website I've specified in the reference link below.

Note that this tutorial is not for beginner, basically I'll just summarized what I did and provide the github repository.

To summarize what I did:
1.) In AppDelegate class, I removed the window definition.
2.) Create a Main story board.
3.) Set the default Main Interface in the project setup, this is the story board we've created in #2.
4.) Add a tableViewController and viewController in the story board.
5.) Create your Todo item object.
6.) Create a customized class for the 2 components we've created at #4.
7.) Code the methods :-)

The source code is available at:

Wednesday, April 16, 2014

How to change varchar to nvarchar in mssql using hibernate

In my recent assignment I was asked to change how hibernate creates a table changing varchar fields to nvarchar. The problem is in MSSQL we need an nvarchar to support UTF-8 characters.

We are actually using a JDBC MSSQL driver available here:

This driver needs to be configured inside jboss, the server I'm on.

First you need to extend SQLServerDialect and override the VARCHAR definition.
package org.kbs.hibernate.dialect;

import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.dialect.SQLServerDialect;

public class MSSQLServerDialect extends SQLServerDialect {

 public UnicodeSQLServerDialect() {
  registerColumnType(Types.VARCHAR, "nvarchar($l)");
  registerColumnType(Types.BIGINT, "bigint");
  registerColumnType(Types.BIT, "bit");


Second you need to tell hibernate to use your new class:
<property name="hibernate.dialect" value="org.kbs.hibernate.dialect.MSSQLServerDialect" />

Tuesday, April 15, 2014

How to get around OptimisticLockException or StaleObjectStateException

The 2 errors above are common specially when dealing with a multi-threaded application. For example in my case I have several rabbit mq listeners. For those that are not familiar with it, it's a server where one language can send a message to be consumed by another but not limited to that. For example communication between .net and java.

This multi-threaded application access a set of web services that update parts of a database. The problem is messages arriving in the rabbit mq bus are asynchronous and are not really aware of each other. So there might be a time that 2 different requests that access the same part of a database at the same time.

Working with this I've remembered 2 different solutions plus I've learned another one:

1.) Using synchronized keyword in a method. This is what I've used, because fortunately all my web service calls are in a single class. Basically it locks the method and only 1 thread can access the method at a time.
public synchronized ClientResponse syncWebServiceCall(
   WebResource webResource) {
 // code here

2.) The next is a statement block, similar to no 1 except that you used a mutex object to lock. The mutex object can be the object where the method is defined or another object. In this way we don't necessarily lock the whole method.
public ClientResponse syncWebServiceCall(
   WebResource webResource) {
 synchronized(this) {
  // code

3.) The last one. Which I've just learned. Using ReentrantLock: Modifying the example:
class X {
 private final ReentrantLock lock = new ReentrantLock();

 public ClientResponse syncWebServiceCall() {
  lock.lock();  // block until condition holds
  try {
   // ... method body
  } finally {