Read/Write System Properties From/Into XML Using Groovy

[ This Blog Was Moved To : ]

Inspired by the following post ( ), I created the following code snippets to Read / Write System properties from/into XML files.

Writing system properties into XML file (output.xml):

def properties =
def fos = new FileOutputStream("output.xml")

Reading system properties from XML file (input.xml):

def prop = new Properties()
def fis = new FileInputStream("input.xml")

And, ofcourse this line can be used to list the properties in any properties object


Here is the resultant XML File:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "">
 <entry key="">Java(TM) SE Runtime Environment</entry>
 <entry key="sun.boot.library.path">/opt/jdk1.6.0_17/jre/lib/amd64</entry>
 <entry key="java.vm.version">14.3-b01</entry>
 <entry key="java.vm.vendor">Sun Microsystems Inc.</entry>
 <entry key="java.vendor.url"></entry>
 <entry key="path.separator">:</entry>
 <entry key="">Java HotSpot(TM) 64-Bit Server VM</entry>
 <entry key="file.encoding.pkg"></entry>
 <entry key="">SUN_STANDARD</entry>
 <entry key="">US</entry>
 <entry key="sun.os.patch.level">unknown</entry>
 <entry key="">groovyConsole</entry>
 <entry key="">Java Virtual Machine Specification</entry>
 <entry key="user.dir">/home/dino/Documents</entry>
 <entry key="java.runtime.version">1.6.0_17-b04</entry>
 <entry key="java.awt.graphicsenv">sun.awt.X11GraphicsEnvironment</entry>
 <entry key="java.endorsed.dirs">/opt/jdk1.6.0_17/jre/lib/endorsed</entry>
 <entry key="os.arch">amd64</entry>
 <entry key="">/tmp</entry>
 <entry key="line.separator">
 <entry key="java.vm.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="">Linux</entry>
 <entry key="tools.jar">/opt/jdk1.6.0_17//lib/tools.jar</entry>
 <entry key="sun.jnu.encoding">UTF-8</entry>
 <entry key="">/home/dino/installations/groovy/groovy-1.6.5/bin/groovyConsole</entry>
 <entry key="java.library.path">/opt/jdk1.6.0_17/jre/lib/amd64/server:/opt/jdk1.6.0_17/jre/lib/amd64:/opt/jdk1.6.0_17/jre/../lib/amd64:/usr/lib64/mpi/gcc/openmpi/lib64:/usr/java/packages/lib/amd64:/lib:/usr/lib</entry>
 <entry key="">Java Platform API Specification</entry>
 <entry key="java.class.version">50.0</entry>
 <entry key="">HotSpot 64-Bit Server Compiler</entry>
 <entry key="os.version"></entry>
 <entry key="user.home">/home/dino</entry>
 <entry key="user.timezone">Africa/Cairo</entry>
 <entry key="java.awt.printerjob">sun.print.PSPrinterJob</entry>
 <entry key="file.encoding">UTF-8</entry>
 <entry key="java.specification.version">1.6</entry>
 <entry key="java.class.path">/home/dino/installations/groovy/groovy-1.6.5/lib/groovy-1.6.5.jar</entry>
 <entry key="">dino</entry>
 <entry key="java.vm.specification.version">1.0</entry>
 <entry key="java.home">/opt/jdk1.6.0_17/jre</entry>
 <entry key="">64</entry>
 <entry key="user.language">en</entry>
 <entry key="java.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="">mixed mode</entry>
 <entry key="java.version">1.6.0_17</entry>
 <entry key="java.ext.dirs">/opt/jdk1.6.0_17/jre/lib/ext:/usr/java/packages/lib/ext</entry>
 <entry key="sun.boot.class.path">/opt/jdk1.6.0_17/jre/lib/resources.jar:/opt/jdk1.6.0_17/jre/lib/rt.jar:/opt/jdk1.6.0_17/jre/lib/sunrsasign.jar:/opt/jdk1.6.0_17/jre/lib/jsse.jar:/opt/jdk1.6.0_17/jre/lib/jce.jar:/opt/jdk1.6.0_17/jre/lib/charsets.jar:/opt/jdk1.6.0_17/jre/classes</entry>
 <entry key="java.vendor">Sun Microsystems Inc.</entry>
 <entry key="file.separator">/</entry>
 <entry key="java.vendor.url.bug"></entry>
 <entry key="">UnicodeLittle</entry>
 <entry key="sun.cpu.endian">little</entry>
 <entry key="groovy.sanitized.stacktraces">org.codehaus.groovy.runtime.
 <entry key="groovy.full.stacktrace">false</entry>
 <entry key="groovy.starter.conf">/home/dino/installations/groovy/groovy-1.6.5/conf/groovy-starter.conf</entry>
 <entry key="groovy.home">/home/dino/installations/groovy/groovy-1.6.5</entry>
 <entry key="sun.cpu.isalist"/>

And this is it for now.


Testing MySQL GET_LOCK() using PHP

[ This Blog Was Moved To : ]

Early this morning, a friend of mine was stuck in GET_LOCK() MySQL function. She was using it in her application. The function was working very well from the MySQL Console , but it was not working from PHP ( Neither from her application, nor from PHPMyAdmin web interface).

She asked my help, so I searched Google, and had a look at the MySQL Documentation about this function.

This function as it appears in the MySQL Documentation, does the following :


Tries to obtain a lock with a name given by the string str, using a timeout of timeout seconds. Returns 1 if the lock was obtained successfully, 0 if the attempt timed out (for example, because another client has previously locked the name), or NULL if an error occurred (such as running out of memory or the thread was killed with mysqladmin kill). If you have a lock obtained with GET_LOCK(), it is released when you execute RELEASE_LOCK(), execute a new GET_LOCK(), or your connection terminates (either normally or abnormally). Locks obtained with GET_LOCK() do not interact with transactions. That is, committing a transaction does not release any such locks obtained during the transaction.

This function can be used to implement application locks or to simulate record locks. Names are locked on a server-wide basis. If a name has been locked by one client, GET_LOCK() blocks any request by another client for a lock with the same name. This allows clients that agree on a given lock name to use the name to perform cooperative advisory locking. But be aware that it also allows a client that is not among the set of cooperating clients to lock a name, either inadvertently or deliberately, and thus prevent any of the cooperating clients from locking that name. One way to reduce the likelihood of this is to use lock names that are database-specific or application-specific. For example, use lock names of the form db_name.str or app_name.str.

mysql> SELECT GET_LOCK('lock1',10);
        -> 1
mysql> SELECT IS_FREE_LOCK('lock2');
        -> 1
mysql> SELECT GET_LOCK('lock2',10);
        -> 1
mysql> SELECT RELEASE_LOCK('lock2');
        -> 1
mysql> SELECT RELEASE_LOCK('lock1');
        -> NULL

The second RELEASE_LOCK() call returns NULL because the lock 'lock1' was automatically released by the second GET_LOCK() call.

If multiple clients are waiting for a lock, the order in which they will acquire it is undefined and depends on factors such as the thread library in use. In particular, applications should not assume that clients will acquire the lock in the same order that they issued the lock requests.


If a client attempts to acquire a lock that is already held by another client, it blocks according to the timeout argument. If the blocked client terminates, its thread does not die until the lock request times out. This is a known bug (fixed in MySQL 5.6).

So, after reading the above wonderful documentation, I thought of several reasons “Why would she not be able to obtain a lock using that function from PHP ? ” and I guessed the following :

  1. She may have called GET_LOCK and never called RELEASE_LOCK and tried to call GET_LOCK again with the same lock name.
  2. She may be calling GET_LOCK from a script where the connection is closed by the end of that script and then tries to check IS_FREE_LOCK from another script.
  3. Something is error in her SETUP ( Which I have no clue how to solve ).

After arguing with her over these points, she proved to me that she is doing every thing the right way. So, I created the following PHP Script to check if there is really and issue in PHP calling GET_LOCK MySQL function.


$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'carpooling';


function LockProjet( $idProjetRevision)
  $sql = "SELECT GET_LOCK('LockedProject_$idProjetRevision', 28800) AS 'GetLock'"; //8 hours lock
  $res = mysql_query($sql);

function IsLockedProjet( $idProjetRevision)
  $sql = "SELECT IS_FREE_LOCK('LockedProject_$idProjetRevision');"; //8 hours lock
  $res = mysql_query($sql);

function ReleaseProjet($idProjetRevision)
  $sql = "SELECT RELEASE_LOCK('LockedProject_$idProjetRevision'); "; //8 hours lock
  $res = mysql_query($sql);

function showValue($res){
  # mysql_close();
 if (!$res) {
    die('Could not query:' . mysql_error());
  while ($i < $num) {
    echo "RESULT IS : $first \n";
    echo "----------------------\n";

When I tested this function on my Laptop, this was the result:

dino@linux-vxrd:~/Documents/php> php locks.php


Which means it should be working fine. So, I asked her to use my code instead, it didn’t work either.
So, the last thing I though of is the privileges of the user she is using to connect to the database, I asked her to open MySQL Administration tool and check user privileges.

So, She checked it, but no use. So, both of us get fed up and she said she gonna do it her own way. I suggested using any ORM Framework, but she got no knowledge about it.
So, This is it.

Wish her best of luck.

All Of you too.