// archives

Tips

This tag is associated with 7 posts

Setting up my development environment on new MacBook Pro

I recently got my new MacBook Pro. I plan to use this laptop for my development (Enterprise Java, Spring, AngularJS, Web et al.) as well as photo editing. This post explains steps I followed to setup my development environment on my new MacBook Pro.
(more…)

MySQL Tip: Can’t create table ‘‘ (errno: 150) message

Recently I was trying to setup a simple database schema in MySQL and was presented with this rather cryptic message:
Can't create table 'test.user_role' (errno: 150).
After scratching my head for long time and trying many options, I finally figured out what was wrong with my SQL script.

The following script resulted in the error mentioned above.


CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(255) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(50) NOT NULL,
  `is_password_temporary` bit(1) NOT NULL,
  `is_enabled` bit(1) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_UNIQUE` (`email`),
  UNIQUE KEY `username_UNIQUE` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `role` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `description` varchar(64) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `user_role` (
  `user_id` int(10) NOT NULL,
  `role_id` int(10) NOT NULL,
  PRIMARY KEY (`user_id`,`role_id`),
  KEY `FK_ROLE_ID` (`role_id`),
  KEY `FK_USER_ID` (`user_id`),
  CONSTRAINT `FK_USER` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `FK_ROLE` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The user_id column in the user_roles table was defined as int(10).
This column points to the primary key id in the user table, which is defined as int(10) unsigned.
This mismatch in the datatype resulted in the error mentioned above.

The following change in the definition of user_roles table fixed the issue.


CREATE TABLE `user_role` (
  `user_id` int(10) unsigned NOT NULL,
  `role_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`user_id`,`role_id`),
  KEY `FK_ROLE_ID` (`role_id`),
  KEY `FK_USER_ID` (`user_id`),
  CONSTRAINT `FK_USER` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
  CONSTRAINT `FK_ROLE` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I wish the error message was little more user-friendly.

MAC OS X Tip: Uninstalling MySQL on Mac OS X

Uninstalling MySQL on Mac OS X involves manually removing following directories and files from your drive.
Assuming your MySQL is installed in /usr/local, perform the following steps:

  1. cd /usr/local
  2. sudo rm mysql removes the symbolic link to the actual mysql installation. You may be prompted for your administrator’s password at this point.
  3. sudo rm -rf mysql-5.1.47-osx10.6-x86/ removes the actual mysql installation. You may have a different version of mysql installed on your system.
  4. sudo rm -rf /Library/StartupItems/MySQLCOM removes the MySQL startup option.
  5. rm -rf ~/Library/PreferencePanes/My* removes preference panes.
  6. sudo rm -rf /Library/Receipts/mysql*
  7. sudo rm -rf /Library/Receipts/MySQL*

I was hoping for a much simpler process for cleanly uninstalling MySQL from my system.
Thanks to Rob Allen for this tip: Rob Allen’s DevNotes

Create your own custom maven archetype

Creating custom maven archetype

Maven provides many archetypes to generate various types of project skeletons. This blog post explains how to create a custom archetype that is tailor-made for your own situation. We will create an archetype for the Struts2 application.
My current development environment includes:

  • Mac OS X 10.6.4
  • Maven 2.2.1
  • Java 1.6.0_20

Create your skeleton project

You need to have a starter project from which new archetype can be created. We can start with the Struts2 application created in the blog post Starting Struts2 web application development (using Maven2 and Eclipse) to create a custom archetype for Struts2 based applications. You can download the zipped source code for this project here.

(more…)

Mac OS X Tip : Accessing /usr/local from the Finder window

Mac OS X hides many files and folders (directories) from the Finder, probably for the security reasons. Typically you don’t need to access these folders and shouldn’t mess with them.

Since I install most of my development related tools (JBoss, Apache Maven, MySQL) in /usr/local directory, sometimes I need to access /usr/local folder from the Finder window (for example, setting up Eclipse environment). This can be easily done by pressing command + shift + G (Go To Folder under Go menu) and then typing /usr/local in the popped-up dialog.

(more…)

Starting Struts2 web application development (using Maven2 and Eclipse)

Before you begin

  1. Follow the blog post Starting web development with Maven and Eclipse to create a web application. This blog shows how we can extend that basic web application to add Struts2 support.
  2. This blog post assumes that you are familiar with Struts2 already. If that is not the case, this would be the good time to do so. The official Apache Struts 2 website is a good place to start. Get a free copy of the mini book Starting Struts 2. Thanks to Ian Roughley and InfoQ.com for making this great mini book available for free.

(more…)

Java Tip: How to use Java Timer to synchronize multiple repeating tasks

Recently I had to implement a service that involved multiple nodes in a cluster to aggregate the data for certain period of time and report it back to the central server. The central server, then aggregated results provided by all the nodes in the cluster. This required all the nodes to align properly when reporting the aggregate data to the central.

For example, if the system was configured to aggregate data for 10 minutes, each node in the cluster was expected to send the aggregate data, every 10th minute beginning at the top of the hour. In this case, the central server was expecting data from all the nodes at 10:00 am, 10:10 am, 10:20 am, 10:30 am and so on. The node started at 10:05:15 am was expected to report its aggregate data at 10:10:00 am and every 10th minutes after that (instead of 10 minutes after the node is started and every 10 minutes after that).
(more…)

Categories