Setup multiple data sources in Grails 2.0

Hi there,

lately I was trying to use multiple data sources with a Grails application. It took me some time to figure out how to do that, therefore I thought it would be something worth sharing. The approach described here will only work for applications written in Grails 2.0 and above since the usage of multiple data sources is a feature which was added into Grails starting with version 2.0.

So here is how you do it. Unsurprisingly you can add your additional data sources within your DataSources.groovy configuration file. You simply add them to the appropriate subsection (development, test, production etc.) within the environments section of your configuration file. That should look some like this:

environments {
    development {
        dataSource {
        //This is your DEFAULT data source
	//Add the configuration parameters for your DEFAULT data source here.
        }

	dataSource_monsters {
	//This is thefirst of your additional data sources, put the configuration
        //parameters  corresponding to the first data source here.
        }

	dataSource_aliens {
	//This is the second of your additional data sources, put the configuration
        //parameters  corresponding to the second data source here.
        }
    }
    test {
	//You can specify other data sources in your test environment as usual.
        dataSource {

	}
	dataSource_monsters {

        }
	dataSource_aliens {

        }
    }
    production {
	//You can specify other data sources in your production environment as usual.
        dataSource {

	}
	dataSource_monsters {

        }
	dataSource_aliens {

        }
    }
}

As you may have noticed, there is a convention on how to specify additional data sources. The convention is to name the section “datasource_[your choosen name]”. So in the example file there are two different data sources, “datasource_monsters” and “datasource_aliens”. The name you choose after the the underscore is totally up to you. Another thing to notice is that you specify your default data source for a specific environment by using the “dataSource” clause without the additional underscore part.

So for each environment – development, test and production we have defined three different data sources. One default data source, and two additional data sources (‘monsters’ and ‘aliens’).

But how do you decide which data source a specific class should use?

The answer is by defining a mapping section within your class. Let’s see how that looks with some domain classes:

class Lair{

}

class Monster{
    static mapping = {
        datasource 'monsters'
    }
}

class Alien{
    static mapping = {
        datasource 'aliens'
    }
}

If a class does not specify a mapping section it will use the default data source defined in your section. So the Lair class would use the default data source for persisting itself. The other two classes (‘Monster’ and ‘Alien’) each define a mapping section with a data source that they would like to use. So the Monster class would be mapped to a monsters table in the ‘monsters’ data source and the Aliens class would be mapped to an aliens table in the ‘aliens’ data source.

I hope you enjoyed the read an everything worked for you. Drop me a comment if you experience any issues or if you think I should add some information to this article.

Advertisements

Python: Getting umask without change

Hi, I recently found myself in the need of getting the umask of a process. After a few searches there seems to be no other way of getting the desired umask then using the umask() system call located within the python os module. The following code seems to be the universally accepted solution to this particular problem:

def getUMask():
    current_umask = os.umask(0)
    os.umask(current_umask)

    return current_umask 

So the solution is to make a first call to umask() and get a hold of the current umask (Line 2). The argument you pass to the umask() call during the first invocation doesn’t matter since you are going to change it back to its original value. After that you make a second call to umask() (Line 3) with the umask you acquired during the first call (Line 2). So this is the point where you change the umask back to its original value. The call to umask is not atomic though, so you have to take this fact into account if you are using multiple threads, this could lead to race conditions.

Even though this is certainly not the holy grail of programming I thought I share this piece of code anyway…

If this post was useful to you, don’t forget to share it with the world…till next time…

Howto Install Tomcat 7 on Debian (Lenny)

This Article describes how to install Apache Tomcat 7 on a Debian (Lenny) OS in 8 easy steps.
For this HowTo to work smoothly you must already have Java installed on your machine. In case you haven’t Java installed on your machine, there is anoter HowTo for this Task on my Blog. Follow the instructions in this Post and come back when Java is up and running.

1. Download it!

The first step is to aquire Tomcat 7 by downloading it from the Homepage. This is really easy, I used wget to download the “apache-tomcat-7.0.2.tar.gz” archive into my /temp Directory. This should look something like this:

cd /tmp
wget http://apache.prosite.de/tomcat/tomcat-7/v7.0.2-beta/bin/apache-tomcat-7.0.2.tar.gz

2. Unzip & Move

Move the package to it’s permanant location and unzip the package into its own folder.

mv apache-tomcat-7.0.2/ /usr/local/tomcat
tar -zxvf apache-tomcat-7.0.0.tar.gz

3. Create “tomcat” Group & User

Next you need to add a new group and a new user to your system. This will be the user and the group under which the Tomcat server runs.

1. To add a group called “tomcat” you simply type:

groupadd tomcat

2. Now you have to create a new user called “tomcat” (useradd tomcat) who belongs to the group “tomcat” (-g tomcat). You also should set the home directory of that user to the directory where you moved the Tomcat server in the previous step. In this case that would be “/usr/local/tomcat” (-d /usr/local/tomcat). So you should end up with a statement that looks something like this:

useradd -g tomcat -d /usr/local/tomcat tomcat

3. Now you should also add the user to the “www-data” group. This group should already exist. You do that by executing the following command:

usermod -G www-data tomcat

4. Create INIT File for Tomcat

Now you should create an INIT-File that makes it possible to start, stop and restart your Tomcat Server. This file must be located in your “/etc/init.d/” directory. You can use the following command to create a file called “tomcat” and open up that file in an editor (I used nano).

nano /etc/init.d/tomcat

Now you should add the following lines into the file an save it:


#Tomcat auto-start
#description: Auto-starts tomcat
#processname: tomcat
#pidfile: /var/run/tomcat.pid

#this path should point to your JAVA_HOME Directory
export JAVA_HOME=/usr/lib/jvm/java-6-sun
case $1 in

start)
  sh /usr/local/tomcat/bin/startup.sh
  ;;

stop)  
  sh /usr/local/tomcat/bin/shutdown.sh
  ;;

restart)
  sh /usr/local/tomcat/bin/shutdown.sh
  sh /usr/local/tomcat/bin/startup.sh
  ;;
esac
   
exit 0

Make sure you set the right paths for the startup.sh and shutdown.sh scripts. They reside in the /bin directory of your tomcat path (use the path to which you moved the tomcat files in step 2).

5. Adjust Permissions of INIT File

Since you have to execute the tomcat file, you have to assign the correct rights for the file to be executable.
This line should do the trick:

chmod 755 /etc/init.d/tomcat

6. Make Tomcat auto-start on boot (optional)

If you want the Tomcat Server to start every time the system boots up you can use the “update-rc.d” command to set a symbolic link at the correct runlevel. For the “tomcat fle” this looks like this:

update-rc.d tomcat defaults

Now the Tomcat Server starts automatically at system bootup. This step is optional you can always start your Tomcat Server manually like this:

/etc/init.d/tomcat start

7. Modify Tomcat Users File

We are almost there! In this step we need to add a user in the tomcat-users.xml. This user is used to gain access to the Tomcat Manager Interface in the next step. So open up the “tomcat-users.xml” file with any editor you like (i used nano):

nano /usr/local/tomcat/conf/tomcat-users.xml

There is a <tomcat-users> section within that file. After the installation this section should only contain comments and look something like this:

<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
</tomcat-users>

Now all we need to do is add a new user by adding some new lines here. After insertion the section should look like this:

<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
  <role rolename="manager"/>
  <role rolename="manager-gui"/>
  <role rolename="admin"/>
  <user username="admin" password="tomcat" roles="admin,manager,manager-gui"/>
</tomcat-users>

In lines 19-22 I added three roles with the names “manager”, “manager-gui” and “admin”. In line 22 I created a user with the username “admin”, the password “tomcat” and the roles I created before. This user will be used to access the Tomcat Manager Interface in the next step.

All there is left to do is to restart the Tomcat Server to make him recognize that the “tomcat-users.xml” file has changed and that there is a new user with the name “admin” and the password “tomcat”. This is how you restart your Tomcat Server:

/etc/init.d/tomcat restart

8. Test Tomcat Manager Interface

Finally we can check if everything went right. If your Tomcat Server runs on your local machine your can access it via the following adress:

http://localhost:8080/

otherwise you have to replace the “localhost” part with the IP adress or name of your server. This could look like this:

http://192.168.6.15:8080/

If all went right you should see the following site in your browser:

This is the Tomcat Startup Site

Tomcat Site

Now you know that your Tomcat Server is running. Next we will log in to the Tomcat Manager Interface. For that you should click on the link that says “Tomcat Manager” in the upper left part of the webpage (This link is actually highlighted in the picture above.). Now you will be prompted for your username and password. Type in the username and the password you set in the “tomcat-users.xml” file (in this case that would be “admin” and “tomcat”)

Tomcat Manager Username and Password Prompt

Tomcat Manager Prompt

After that you should see the site of the Tomcat Manager.

The Tomcat Manager Interface

Tomcat Manager Interface

Congratulations you just installed your very own Tomcat Server.

Let me know what you think about my article. Perhaps there are some sections where i need to be a little bit more elaborate. Just drop me a line….

Howto Install Sun’s Java 6 on Debain

This article is a short Tutorial on how to install Java 6 on a Machine running Debian (Lenny) OS. I will try to keep the Installation process as simple as possible so let’s get started…

Step 1: Add “non-free” parameter

If you haven’t already done so, you have to add the “non-free” parameter to your apt “sources.list”. So open up your sources.list normallly located in /etc/apt/sources.list like so

nano /etc/apt/sources.list

…if you did not modify your “sources.list” up to now it might look something like this…

# deb cdrom:[Debian GNU/Linux 5.0.4 _Lenny_ - Official i386 NETINST Binary-1 20100201-16:45]/ lenny main
# deb cdrom:[Debian GNU/Linux 5.0.4 _Lenny_ - Official i386 NETINST Binary-1 20100201-16:45]/ lenny main

deb http://ftp.de.debian.org/debian/ lenny main
deb-src http://ftp.de.debian.org/debian/ lenny main

deb http://security.debian.org/ lenny/updates main
deb-src http://security.debian.org/ lenny/updates main

…now you have to add the “non-free” parameter to the end of the deb and deb-src lines. After that your “sources.list” should look like:

# deb cdrom:[Debian GNU/Linux 5.0.4 _Lenny_ - Official i386 NETINST Binary-1 20100201-16:45]/ lenny main
# deb cdrom:[Debian GNU/Linux 5.0.4 _Lenny_ - Official i386 NETINST Binary-1 20100201-16:45]/ lenny main

deb http://ftp.de.debian.org/debian/ lenny main non-free
deb-src http://ftp.de.debian.org/debian/ lenny main  non-free

deb http://security.debian.org/ lenny/updates main
deb-src http://security.debian.org/ lenny/updates main

…save your modified “sources.list” file.

Step 2: Install Java

Before you can install the sun-java packages you have to update your package repository…

apt-get update

now you can install the java packages…

apt-get install sun-java6-jdk sun-java6-jre

Step 3: Make Sun Java the preferred Java runtime

You can make the installed Java distribution the preferred Java runtime by using the “update-java-alternatives” command like so:

update-java-alternatives -s java-6-sun

This will make the newly installed Java runtime the standard Java runtime for your system.

Step 4: Set JAVA_HOME

Before we do anything else we set the JAVA_HOME environment variable so that other applications can find your java distribution. To to that you have to edit your “.bashrc” file located in your home directory. So open up your “.bashrc” file:

nano ~/.bashrc

…and add the following line to the end of your file

export JAVA_HOME=/usr/lib/jvm/java-6-sun

at this point I’m assuming that your distribution installed java in /usr/lib/jvm/java-6-sun which is the default behaviour. If that is not the case you have to replace the path with your installation path. Now you can try to log out and log in again to check if the JAVA_HOME is correctly set. After logging in again you just type…

echo $JAVA_HOME

if everything went right you should see the path that you set in your “.bashrc” earlier.

/usr/lib/jvm/java-6-sun

The approach discussed above will set the JAVA_HOME environment variable for your user only! So other users on the system cannot make use of your JAVA_HOME variable. To set the JAVA_HOME variable on a systemwide scale you have to edit the /etc/profile file. Setting the JAVA_HOME variable in the /etc/profile file will make it available to every user of the current system. The process of setting the JAVA_HOME variable is the same as described above, except that you use the /etc/profile file instead of the ~/.bashrc file in your users home directory.

Step 5: Check your Java Installation

If everything went right the command

java -version

…should yield the following output:

java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

… so that’s that…hope it helps…cya

Variable LIMIT statement in MySQL

Question:

Is there a way to use variables in conjunction with the SQL LIMIT clause?
(e.g. SELECT * FROM movies LIMIT @foo)?

Answer:

No not directly…at the time of writing this is still a big issue of MySQL Development. Nonetheless there exist various workarounds. I will discuss a few of them in this article.

Description:

The main issue is that you want to construct an SQL query that hands a variable as a parameter to the LIMIT clause. That could look something like this:

SET @foo = 5;
SELECT * FROM movies LIMIT @foo;

This immediately results in the following error message.

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘@’ at line 1)

So to all appearances SELECT queries using the LIMIT clause do not work when a SQL variable (e.g. @foo) is used as a parameter in the LIMIT clause.

Solutions:

In the following I present a few workarounds to resolve this little handicap:

1. Make use of Prepared Statements

SET @foo=5;
PREPARE STMT FROM 'SELECT * FROM table LIMIT ?';
EXECUTE STMT USING @foo;

You could also concatenate your entire SELECT Statement and convert this to a Prepared Statement. In the following example the parameter “sParameter” is the used variable.

SET @myQuery = CONCAT('SELECT * FROM test ORDER BY ', sParameter);
PREPARE stmt FROM @myQuery;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

If you use queries like these a lot, it might make sense to wrap the whole thing up in a stored procedure:

DELIMITER $$

CREATE PROCEDURE `get_movie_range`(
IN _FROM INTEGER,
IN _TO INTEGER
)
BEGIN
PREPARE STMT FROM "SELECT * FROM movies LIMIT ?,? ";
SET @FROM = _FROM;
SET @TO = _TO;
EXECUTE STMT USING @FROM, @TO; 
END$$

With the stored procedure in place you can make calls like:

CALL get_movie_range(7,(SELECT COUNT(*) FROM movies));

2. Make use of variables and the BETWEEN Statement

This statement selects all rows beginning with 12 and ending at 20.

set @i=0;
select * from movies where (@i:=@i+1) between 12 and 20;

So that’s it…hope it helps…:)

SELECT TOP in MySQL

Question: Is there a way to emulate the behavior of SELECT TOP [Integer] … in MySQL?

Answer: Yes there is…you have to make use of the LIMIT clause.

Example:

The query…

SELECT TOP 5 * FROM movies;

Output:

titleID title url
0012349 The Kid http://www.imdb.com/title/tt0012349/
0015864 The Gold Rush http://www.imdb.com/title/tt0015864/
0017136 Metropolis http://www.imdb.com/title/tt0017136/
0017925 The General http://www.imdb.com/title/tt0017925/
0018455 Sunrise: A Song of Two Humans http://www.imdb.com/title/tt0018455/

…will look like this in MySQL…

SELECT * FROM movies LIMIT 5;

Output:

titleID title url
0012349 The Kid http://www.imdb.com/title/tt0012349/
0015864 The Gold Rush http://www.imdb.com/title/tt0015864/
0017136 Metropolis http://www.imdb.com/title/tt0017136/
0017925 The General http://www.imdb.com/title/tt0017925/
0018455 Sunrise: A Song of Two Humans http://www.imdb.com/title/tt0018455/

You can find more information about selections with LIMIT here.

Hope this helps…