Grails, MySQL : Cannot create PoolableConnectionFactory

[ This Blog Was Moved To : http://www.diknows.com ]


Ok, You read this post I assume you face the same problem I faced.

Quick Solution :

If this is your MySQL Connection URL :

“jdbc:mysql://localhost/DN_NAME”

Change it to

“jdbc:mysql://127.0.0.1/DB_NAME”

Understanding the solution :

Recently, I am developing a grails application. I was using the default in memory HSQLDB, as it is wonderful for changing the database design frequently. But, as I felt stability in the project I wanned to move the project to the next level where data get persisted. Hence, I tried to move to MySQL Database by modifying the DataSource.groovy File.

dataSource {
pooled = true
//    driverClassName = “org.hsqldb.jdbcDriver”
//    username = “sa”
//    password = “”

// MySQL Database configurations
driverClassName = “com.mysql.jdbc.Driver”
username = “root”
password = “123456”
}

And result was a very long exception

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘transactionManager’: Cannot resolve reference to bean ‘sessionFactory’ while setting bean property ‘sessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sessionFactory’: Cannot resolve reference to bean ‘hibernateProperties’ while setting bean property ‘hibernateProperties’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘hibernateProperties’: Cannot resolve reference to bean ‘dialectDetector’ while setting bean property ‘properties’ with key [hibernate.dialect]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dialectDetector’: Invocation of init method failed; nested exception is org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Communications link failure

So, I tried to Google on PoolableConnectionFactor .. But Results was either

Change /etc/hosts.allow and check /etc/hosts

or

Some changes in /etc/my.cnf and grant access to the database user

And they all failed. So I created a new Java Project in NetBeans IDE, added the JDBC Driver Jar file, and wrote the following code:

String dbUrl = “jdbc:mysql://localhost:3306/DB_NAME”;
Class.forName ( “com.mysql.jdbc.Driver” );
Connection connection = DriverManager.getConnection ( dbUrl , “root” , “123456” );

So, the following single exception appeared :

Dec 5, 2009 9:02:41 AM testmysql.Main main
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

So, I Googled again and I found this post

http://forums.sun.com/thread.jspa?threadID=5339538

Which is long, and here is the main point of it :

Re: Communication link failure!!!!
Mar 16, 2009 3:49 PM (reply 8 of 12)  (In reply to #7 )
I’m happy to say that I resolved the issue. I replaced localhost with 127.0.0.1 and it worked perfectly. I’ve also checked what the browser gives as localhost and it’s the IPv6 localhost (to anyone who might be experiencing the same issue that’s 0:0:0:0:0:0:0:1) not the IPv4 one, so that was the problem. Not sure why this machine defaults to IPv6 though.

So, It seams the PC is defaulting to IPv6 not IPv4

and the solution is as I said above

Change the MySQL Connection URL from localhost to 127.0.0.1 (or whatever your localhost IP Address is )

NOTE: you can find you localhost IP Address in /etc/hosts

NOTE: If you use openSuse Linux ( Like I Do ) and you want to disable IPv6, you can do so using

Yast-> Network Manager

Wish you best of luck.

———————————————————————————————

New Updates :

I had another PoolableConnectionFactory exception. This time because I had reinstalled my Linux system, so my databases were gone. I got the following exception

Cannot create PoolableConnectionFactory (Unknown database ‘DB_NAME‘):
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database ‘DB_NAME

Because, there was no Database scheme in the mysql database engine. So, to solve this:

  1. In console window type “mysql -u root -p” if your mysql username is root and you have a password.
  2. Create new database using “create database DB_NAME”.
  3. Exit the mysql console as you don’t need it, using “exit” command.

Now, you can restart your grails application and this exception will not show up again.

Wish you best of luck.

Advertisements

Shell script to count number of Mega Bytes used in PPP connections

[ This Blog Was Moved To : http://www.diknows.com ]


Recently I bought, a USB Stick modem that is limited to 500 MB, after the 500 MB each 3 MB = 1 LE. For that I needed to create a counter to check my usage .. not to exceed the 500 Limit. so here it is :

#!/bin/bash

while :;
do t=`pppstats -a | grep -v IN | cut -c -10` ;
echo 'Created By Dino.'
echo '--------------------------------'
echo "Number Of Megabytes used is : ";
echo ${t}/1024/1024 | bc ;
echo '--------------------------------'
sleep 10;
clear;
done

And This is it .. I used till now 166 MBs.. I like that..

======================

New Updates : Lets call it version : 0.2

——————–

#!/bin/bash
###########################################################
#
#        Created By Diaa Mohamed Kasem - Fall 2009
#
###########################################################
#The Main Directory, 
#where ppp counter related data are stored.
mainDir="/root/.pppCounter"
#The Data File where the latest bytes count are stored
dataFile="$mainDir/data.txt"
#The temporary variable holding the ppp IN bytes count
currentValue=0
#Create the main directory if it doesn't exist
mkdir -p $mainDir

# If the data file exists
if [ -f $dataFile ]; then
  # Get the latest saved bytes count 
  currentValue=`cat $dataFile`
  # Inform User
  echo "Latest Used Value is : $(($currentValue/1024/1024))"
fi

# Infinit loop
while :; 
  # Get the current bytes count downloaded 
  # since injecting the stick modem
  do t=`pppstats -a | grep -v IN | cut -c -10` ;
  # Inform User
  echo 'Created By Dino.'
  echo '--------------------------------'
  echo "Number Of Megabytes used is : \
  $(($t/1024/1024 + $currentValue/1024/1024 )) MB";
  echo '--------------------------------'
  # Save the data in the data file
  echo $(($t+$currentValue)) > $dataFile
  # Sleep for 10 seconds
  sleep 10;
  # Clear the screen
  clear;
done

Note : This should run as root

And here is the result till now :

Created By Dino.
——————————–
Number Of Megabytes used is :   97 MB
——————————–

Hope it is helpful..