dimanche 4 janvier 2015

open Mysql connection for long time to execute queries in block


I am writing one script which execute bunch of queries in block. here block means version number, each version have different different queries.


I write two shell script.



1 version.sh


version.sh contains functions with version number in which bunch of queries are written.



#!/bin/bash
#. /home/sachin/db_version/call_version.sh

user=xxx
password=xxxx
host=xxx.xxx.x.xxx

# Connection string for MySQL
connect=" mysql --host="$host" --user="$user" --password="$password" -s -N "


version_1()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
insert into city values('Rajkot',0001);

QUERY_INPUT
}

version_2()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
insert into city values('Ahmedabad',0002);
QUERY_INPUT
}

version_3()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
insert into city values('Surat',0003);
QUERY_INPUT
}

version_4()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
insert into city values('Vadodara',0004);
QUERY_INPUT
}

version_5()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
insert into city values('Jamnagar',00051);
insert into city values('Jamnagar',0005;
insert into city values('Jamnagar',0005);
QUERY_INPUT
}


Another script is call_version.sh which execute above functions.



2 call_version.sh

#!/bin/bash
. /home/sachin/db_version/version.sh

user=xxxx
password=xxxx
host=xxx.xxx.x.xxx



# Connection string for MySQL
connect=" mysql --host="$host" --user="$user" --password="$password" -s -N "

# If all functions in version.sh goes right then this function call.
version_update()
{
$connect <<QUERY_INPUT
use ucroo_cloud;
update schema_db set count = $initial_version;
QUERY_INPUT
}


# Get the initial version number from database.
initial_version=$(mysql --host=$host --user=$user --password=$password -s -N <<QUERY_INPUT
use ucroo_cloud;
SELECT count FROM schema_db;
QUERY_INPUT
)
echo $initial_version

# Get the Last version number from version.sh file.
last_version=$(grep version_ version.sh | tail -1 | sed '/_/!d;s//&\n/;s/.*\n//;:a;/()/bb;$!{n;ba};:b;s//\n&/;P;D')
echo $last_version

$connect
# Run a loop from initial version number to last version number.
while [ $initial_version -le $last_version ]; do
version_$initial_version # Call function
if [ "$?" -eq "1" ]; then
echo "Something went wrong with version_$initial_version"
echo $(date) " Something went wrong with version_"$initial_version'()' >> /home/sachin/db_version/version_log.log
version_update
break
else
if [ $initial_version -eq $last_version ]; then
version_update
break
else
initial_version=`expr $initial_version + 1`
fi
fi
done


Explanation of both script :


Using call_version.sh file first I get initial version number from the db and after that I get last version number from version.sh.


Now I want to execute functions from initial version number to last version number.


Above both script working fine for me but my problem is I have open MySQL connection every to execute each function. I want to open only one time MySQL connection and execute each functions.


I don't know how to do this, I tried every thing but did not get right solution.


Please if any one have any idea then let me know or if there is any other way to do my requirements then please let me know.



Aucun commentaire:

Enregistrer un commentaire