Sunday, August 21, 2011


As long as I know myself to be a coward I shall be unhappy.

L. Frank Baum (1856-1919)

A qn in facebook

How many ways I can keep 24 balls into 4 boxes, each box should atleast contain 1 ball, and the total number of balls in the boxes should be 24?
I think the ans is : 24 * 23 * 22 * 21 + (20!); any thoughts?

how did i arrive at this ans?
since its said every box should have atleast 1 ball lets first distribute 1 ball per box i.e.. the total ways of doing this is: 24C1 * 23C1 * 22C1 * 21C1

now i an left with 20 balls. so the question modifies to distributing the 20 balls among 4 boxes any number can go into any box; which is same as saying: i have 20 places distribute 20 balls and the ans is: 20!; hence the answer

Sunday, August 14, 2011

Logging queries in mysql

At times when debugging ORMs lke hibernate we may need to know what queries are actually hitting the db. MySql gives an easy way to do this. Add the below in the my.cnf file

  1. log=/var/log/mysql/general.log
  2. place the above in the section [mysqld] and
  3. restart the mysql: /etc/init.d/mysqld restart;

MYSQL: logging with out userid and pwd

at times when u import the db it may erase ur userid and pwd. When the java tries to connect to the db it throws exception that this ip is not allowed to connect to the db; in such a case use the below fix. it removes the necessity to have userid and pwd to loginto the mysql

  1. skip-grant-tables under the section [mysqld]
  2. restart the mysql: /etc/init.d/mysqld restart

be careful that u dont do this on production dbs

MySQL:Got a packet bigger than 'max_allowed_packet' bytes

This is generally the case when u try to import a database from a dump. The fix is to increase the max_allowed_packet size follow the below steps

  1. vi /etc/my.cnf
  2. add: max_allowed_packet=1G under the section [mysqld] and save the file
  3. restart the mysql: /etc/init.d/mysqld restart;
  4. it should be smooth, if at all u get a warning telling it will ignore the cnf file, then try
    1. chmod 655 /etc/my.cnf and again try to restart.

This should do.

Friday, August 12, 2011

egrep–useful linux command


suppose we want to search for a string “abc” in a file then we use

grep “abc” /path/to/file/file.txt

but what if u want to search for either “abc” or “xyz”? u need to grep twice. instead use egrep

egrep “abc|xyz” /path/to/file/file.txt

the best use of egrep comes in when we tail a file and want to search for a set of strings in the tail eg:

tail –f /path/to/file/file.txt | egrep “abc|xyz”

Monday, August 8, 2011

Transaction issue :: hibernate

getSession() –> the java doc says it will create a new session/ continue with the existing one based on getHibernateTemplate().setAllowCreate(…); set to true/ false.

How ever, i found the things to be diffarent. I had a 2 DAOs one executing: getHibernateTemplate().find(..); the other using getSession().createCriteria(..); They are wrapped by the same transaction interceptor. What i found is when the 2nd DAO runs the hibernate throws illegal state exception.

Why?? The getSession() always tries to crate a new session, if the setAllowCreate is set to false, then it throws illegal argument exception. Why this devaiation??? as always only god knows it.

To get the current session in 2nd DAO, i found the fix: getHibernateTemplate().getSessionFactory().getCurrentSession(); and things went off very fine.

Friday, August 5, 2011

Adding complex and & Ors in hibernate

Recently i had to do a query as:

select * from t_table where (t.a=’a’ and t.b=’b’) or (t.a=’aa’ and t.b=’bb’). One way to get over this is via HQL query… which i hate.. i wanted to do it using criterias… finally Conjunction and disjunctions came for resque. Below is the code.

Session session = getSession();
try {
Criteria crit = session.createCriteria(Table.class);
Disjunction conMerOrs = Restrictions.disjunction();
for (AB ab: ABList) {
Conjunction ands = new Conjunction();
ands.add(Restrictions.eq("a", ab.a()));
ands.add(Restrictions.eq("b", ab.b));
crit = crit.add(ors);
return crit.list();
} finally {

let me know if it helped