Java Developer

MySQL issue with N and Ñ treated as equal value

I encountered and issue today where I have a table of person records. Some last name have values of IBAÑEZ and some have IBANEZ.

Grails - How to Create Custom Table Index or Composite Index in Grails

Domain classes in Grails have default index on Primary Key and Foreign Key, as they are usually used to join tables. But only these are created by default. If you wish to create a custom index given a field and index name, the syntax is simple. Here is an example:

Grails AJAX tutorial - formRemote and submitToRemote tags

The tag formRemote is easy to use. If you have an existing form that is not using AJAX, it can easily be converted without requiring any change in controller code.

Java and NGINX Microcaching - Benchmark Results

Utilizing the caching features of NGINX will boost your Java Application performance. It was shown in the previous post how to configure microcaching with NGINX and Tomcat or other Java application server. This post will show the benchmark results of how much can be potentially gained.

Fast Fuzzy String Matching with Grails/Hibernate and MySQL

The popular fuzzy string matching methods are Levenshtein distance, Soundex, Metaphone and Double Metaphone. I created a post on Levenshtein distance here. But the performance is very disappointing. Scanning 40,000 records took around 25 seconds.
An alternative is Soundex. It has some deficiencies in terms of accuracy, specially if the language is not English. However, since this is the most widely known phonetic algorithm, it has native support in MySQL. It is also extremely fast. When I switched algorithms, the query ran from 25 seconds from Levenshtein, down to a fraction of a second for Soundex.

Java and NGINX Microcaching - An Example of speeding your Tomcat deployments

Many times we do very complicated things just to make our application handle more transactions. For example:
  • Tuning database design and queries to lighten database work
  • Use of table index and partitions for efficient database operations
  • Database clustering to increase throughput of database server
  • Caching of database results on the Java application level, to reduce communication and also database work.
  • Clustering of application server
And there are many others.

Grails fuzzy string matching with MySQL and Levenshtein distance

This is a hackish approach for fuzzy string matching in Grails, as an alternative to searchable plugin, assuming MySQL database is used.
As an introduction, the levenshtein distance is used on how similar two strings are. It computes the minimum number of character substitution that has to be done, in order to convert one string from the other. For example, if the levenshtein distance is 0, it means the two strings are exactly the same. If levenshtein distance is 1, it means there is only 1 character differing the two strings. We can use this function for fuzzy matching using a very small treshold for the distance.

Grails AJAX tutorial - remoteFunction tag

The tag remoteFunction is one of the most powerful tag in Grails. Because of it's simplicity and flexibility, you can use it as building block for building complex interface behavior. Check out also my post on remoteLink tag as they share many similarities.

Grails AJAX tutorial - remoteField tag

The remoteField tag is a simple tag used to render a text field that sends changes to it's value back to the server. Simple usage bellow:

Grails AJAX tutorial - remoteLink tag

Grails is a very nice platform to program AJAX applications. It supports the feature out of the box. The API is very simple and easy to grasp where you only need to learn the 5 basic tags:

Adding SQL logging to your Grails application

While ORM is great in simplifying our work with the database, it is still very useful to know SQL statements our application is throwing at the database. Specially when we are still developing the application.

Understanding Grails dbCreate or hbm2ddl

One of the convenient features of Hibernate that is inherited by Grails, is that once you have defined your data model, it can create the tables for you automatically on start up of the application. The nice thing about this is since Hibernate/GORM is database agnostic, it means you don't need to know how to define the tables in different relational databases. Be it Oracle, DB2, or MySQL. If you change your connection property, and load the appropriate JDBC driver, it will know how to talk to the database and create the correct tables.

Specifying MyISAM or InnoDB MySQL storage engine in Grails

MySQL is a popular choice of database for many developers. It's open source, easy to install, stable, and very fast. However, MySQL comes with several storage engine and you need to decide which one to choose. The two most popular are:
  • MyISAM - this is the default storage engine for MySQL prior to version 5.5. I believe this is the fastest storage engine of all, but the drawside is it does not support transactions.
  • InnoDB - this is not as fast as MyISAM, but it support transactions. This is the most popular among Java/Grails developers.

Changing the context name of a Grails application

By default, the context name of your application is the same as the name you supplied when you run "grails create-app". For example:

Configuring MySQL JDBC connection for Grails

During development, I recommend using a standalone database server like MySQL, instead of using the default HSQLDB or H2 database. Because you can open a client and inspect the table structure created, and also perform queries outside the Grails application.
To configure MySQL in your datasource, first download the driver from:

Changing Grails homepage to a particular controller and action

Changing the homepage of a Grails application is very simple. But sometimes simple questions are hard to research and Google. That is why I like collecting mini howtos of many things I encounter in my everyday programming.
The default code for the home is in the GSP file $PROJECT_HOME/grails-app/views/index.jsp

Restore mysql backup with progress bar

I wanted to restore a large production database before in my home computer, to replicate a particular bug. Since the database dump is so large, it took a lot of time. Around 30 minutes. The issue with such operation is that you don't know if the MySQL process hanged-up, or if it is still normally progressing. A good solution is to have a progress bar.

Preparing a Linux development server

Regardless if you are working on a team or as a single developer, it is beneficial that you set-up your own development server. You can use this for code repository, project management tool, or application server. For single developer, or for those working remotely with a team, renting a dedicated server or VPS is a good choice. Because you can access your server in any location.

Simple Git project repository using just git and ssh

When git was new, gitosis is commonly used to manage git-based projects. At the present, gitolite has replaced gitosis in popularity. But if you are just a single developer, or a small team with very few projects, you don't need any of these. Just plain git and ssh is enough.
First, you will need to setup a development server. You can read and follow this tutorial to setup a debian-based server. To recap, you need at a minimum to setup a password-less login to your server.

Using Tomcat JNDI datasource when deploying Grails production war

When developing a grails application, it is acceptable to hard code the database connection in DataSource.groovy. But when you want to create a war and deploy it on an application server, it is better to use JNDI. This is more flexible because you can change the connection property in the application server, instead of creating a new war.

How to setup your linux machine for grails development

Since this is my first post, I want to start with something very simple yet useful. Usually, our first hurdle when studying a technology or framework is to setup our development environment. Assuming your machine is Linux based, this is how to prepare your machine. For this guide, my operating system is Linux Mint 14 Cinnamon, which is based on Ubuntu 12.10

Configuring MySQL for faster development performance

The default configuration of MySQL is not very fast. They made it generic so that it can run on servers with small memory (For example, a VPS with 256Mb RAM). But desktop nowadays have so much memory and disk space that we can trade off for faster performance.
Performance is important because it affects your productivity. I have experimented with many configurations, but below is the simplest that I came up with that gave me good results.

Configuring NGINX in front of Tomcat or other Java application server

NGINX is an awesome software that functions as both web server and as a reverse proxy. Sometimes you may need to configure it in front of your tomcat or other application server. There are many reasons but here are some common ones: