June 2012 Posts

Setup Apache Tomcat and Ubuntu VM on Windows Azure

Recently i got a project which need me to deploy on a cloud infrastructure. After spending some time finding the right cloud provider, finally i choose Microsoft’s product Windows Azure. According to Wikipedia, Windows Azure is a Microsoft cloud computing platform used to build, host and scale web applications through Microsoft data centers.

Actually, i was a little suprised because Microsoft provided Ubuntu Server images for its VM OS selection. I tought the only OS available for Mcrosoft’s cloud platform would be from windows server family.

Okay, first i start by picking New Virtual Machine,and pick Ubuntu from list of Virtual Machines.

Next is selecting your VM name, username and password, and your virtual machine’s size, For this example, im trying a small size server.

Select your DNS name and region and your availability set. Finish your configuration, and start creating your Ubuntu VM on Azure.

Next is connect to your Ubuntu via SSH, and start downloading apache tomcat using this command.

sudo apt-get install tomcat7

It will install Apache Tomcat 7, and start your tomcat using this command,

sudo services tomcat7 start

check whether your Tomcat’s port (8080) has opened yet or not,

netstat -aon | grep 8080

Next is adding your tomcat port to your VM’s endpoints.

After youve add your endpoint, it’s time to test your tomcat server. Try accessing your public virtual ip address via browser and see whether your Tomcat Server is ready. ;-)


Create A Database Transaction Using MyBatis

According to Wikipedia, transaction comprises a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions.

Transactions in a database environment have two main purposes:
1. To provide reliable units of work that allow correct recovery from failures and keep a database consistent even in cases of system failure, when execution stops (completely or partially) and many operations upon a database remain uncompleted, with unclear status.
2. To provide isolation between programs accessing a database concurrently. If this isolation is not provided the programs outcome are possibly erroneous.

So basically, the concept of transaction is all-or-nothing. Whether all the query on transaction succesfully executed, or none of them executed.
On this example im trying to insert 5 datas, but on the 5th data i will throw an exception. If the database transaction is running well, none of the datas is on the database.
And dont forget, if you are using MySQL, please make sure that your table engine is InnoDB.

First, as always, i’ll start with a simple MySQL table,

CREATE TABLE `testing` (
  `name` varchar(30) NOT NULL,
  `address` varchar(255) NOT NULL,
  UNIQUE KEY `ix` (`name`)

Next is a java bean,

package com.edw.bean;

public class Testing {

    private Integer id;
    private String name;
    private String address;
	// other setter and getter
    public String toString() {
        return "testing{" + "id=" + id + ", name=" + name + ", address=" + address + '}';

Next is creating xml file for database configuration,

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    <environments default="development">
       <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="UNPOOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost/test"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
    <mappers />        		

A java class to load my database configuration

package com.edw.config;

import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisSqlSessionFactory {

    private static final SqlSessionFactory FACTORY;

    static {
        try {
            Reader reader = Resources.getResourceAsReader("com/edw/sqlmap/Configuration.xml");
            FACTORY = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e){
            throw new RuntimeException("Fatal Error.  Cause: " + e, e);

    public static SqlSessionFactory getSqlSessionFactory() {
        return FACTORY;

And a java interface for insert queries,

package com.edw.mapper;

import com.edw.bean.Testing;
import org.apache.ibatis.annotations.Insert;

public interface TestingMapper {
    @Insert("INSERT INTO testing values(null, #{name},#{address})")
    public void insert(Testing testing);

This is the main class for my application

package com.edw.main;

import com.edw.bean.Testing;
import com.edw.config.MyBatisSqlSessionFactory;
import com.edw.mapper.TestingMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

public class Main {

    private Logger logger = Logger.getLogger(Main.class);
    public Main(){        

    private void start(){
        logger.debug("==== BEGIN ====");
        SqlSessionFactory sqlSessionFactory = MyBatisSqlSessionFactory.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TestingMapper testingMapper = sqlSession.getMapper(TestingMapper.class);
        try {
            Testing testing = new Testing();
            testing = new Testing();
            testing = new Testing();
            testing = new Testing();
            testing = new Testing();
            // try to create an exception
                throw new Exception("No Tebek allowed");
            logger.debug("=== END =====");
        } catch (Exception e) {
        } finally{
    public static void main(String[] args) {
        new Main().start();

It supposed to show exception on your Netbeans console, and your data wont be on your database.

and last but not least, my log4j properties

log4j.rootLogger=DEBUG, stdout

log4j.appender.stdout.layout.ConversionPattern=%d %c:%L - %m%n

This is the screenshot for my netbeans project structure,

Cheers (D)


Weird “Unparseable Date” Exception When Using Java’s SimpleDateFormat Class

I found this weird error at my production server, an error which somehow never show up at my testing environment.

java.text.ParseException: Unparseable date: "Thu Oct 11 00:00:00 ICT 2012"
        at java.text.DateFormat.parse(DateFormat.java:337)
        at DateClass.main(DateClass.java:7)

It seems that my application is unable to parse the string i provided. But, what makes me confuse is, i try with the same class on my laptop over and over again but this error is never show up. I tought it has something to do with the server’s JDK version or even the server’s operating system.

This is my java class that i use for testing purpose, it runs very well on both my laptop and development server, but it shows error on my server.

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateClass {
    public static void main(String[] args) throws Exception {
        String date = "Thu Oct 11 00:00:00 ICT 2012";
        Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(date);

After spend several minutes googling, i found out that it happen because of Java’s Date Localization. This is my workaround,

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateClass {
    public static void main(String[] args) throws Exception {
        String date = "Thu Oct 11 00:00:00 ICT 2012";
        Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(date);

Another workaround, if you are using Apache Tomcat, is adding this configuration on your Tomcat’s server parameter.


Hope it helps others ;)