Tuesday, September 19, 2006

Hibernate session.

Hibernate session management is a little tricky for new users.
There are two ways to obtain a session:
sessionFactory.getCurrentSession(); and

The first way requires the following line in the hibernate.cfg.xml:
<property name="current_session_context_class">thread</property>

Seems if you use the first method, when you commit a transaction by calling tx.commit(), the session will be closed automatically. This can introduce error if you try to close the session again.

Whereas if you use the second method, a transaction will not close the session after commit, you will have the chance to manually close the session.

The above comments applies for hibernate 3.1, I used it with Tomcat5.5 datasource on Java1.4

Friday, August 25, 2006

why need to synchonize on an object before calling wait method

A very nice answer I found online:

Just to reinforce things. The reason to call wait() is because you are waiting for a particular state to be achieved - which will be signified by someone calling notify or notifyAll. That state has to be, by definition, shared mutable state (it is shared between you and the notifier thread and it must be mutable or you will be waiting a very very very long time). As you know access to shared mutable state must always be performed while holding a suitable lock (AtomicXXX and lock-free concurrent collections not-withstanding). So you have to hold the lock to check the state you are going to wait() upon. The wait() atomically releases the lock and suspends the thread. The notification thread then acquires the lock, modifies the state and calls notify()/notifyAll(). At that point the wait()ing thread is removed from the wait-set and is now waiting to reacquire the lock that the notifying thread may not have yet released. When the waiting thread returns from wait() it owns the lock again and should then re-check the state to see if the new state is what it is waiting for. If it is then, generally, while still holding the lock the thread performs some action that relied on the object being in the state it waited for.

Note that wait() must always be called in a loop testing the condition being waited upon. Spurious wakeups are permitted and do occur.

To me, I think the reason of requiring the synchonization is to ensure the atomicity of "checking the status and wait" operations. So that no other thread can change the status in the middle.

Tuesday, August 08, 2006

xsd:all tag

I am using xsd schema to validate xml files and using xsd:all tag to group elements. Based on w3 documents, the default minOccurs of an element in xsd:all should be zero. (meaning it can just not show up without explicitly set its minOccurs to zero). But I found this is not true for any xsd validators I tested (XML spy, xerces SAXParser). I have to set the minOccurs to zero for each element if I want it to be optional.
What makes thing worse is the misleading parsing error message when I am using xerces packages in java. I started testing adding minOccurs attributes to some elements to see if this will solve the problem. However the error message does not change, i.e. the element I just added 'minOccurs=0' still seems mandatory based on the error message. I guess this is a bug in the xerces package. Only utill I fixed all the elements, the error message is gone. Whereas in XML spy, it's much better. Whenever I added minOccurs for an element, I can see it is no longer required when I try to validate the xml. This is how I figured the default value of minOccurs for elements in xsd:all group is one.

Tuesday, August 01, 2006

Install PHP5 with Apache2 and mysql on windows

Today I installed php5 on my working desktop to edit my baby's website: www.qqvivian.com.
Here is my experience:
Windows + Apache 2 + PHP 5 installation (This only applies to Apache 2.0.x, for 2.2.x, the dll doesn't work)

1. Install Apache.
2. Unzip PHP to a directory, add it to path and PHP_HOME
3. Copy php\php.ini-recommended to windows\php.ini
4. Edit php.ini, set doc_root to apache htdocs directory
5. Edit php.ini, set extension_dir, uncomment extension=php_bz2.dll.
6. Install php as Apache module: Add the following lines to "httpd.conf" file under apache conf directory.
LoadModule php5_module "php/php5apache2.dll"
AddType application/x-httpd-php .php
7. Add test page phpinfo.php, restart Apache to see.

Everything is easy, except one tricky point. The above steps work only for Apache version 2.0.x, it will not work for latest 2.2.x. Obviously the php dll files are not fresh for 2.2.x.

After that, I try to configure php working with existing mysql database:
Still everything is clear, two steps involved: uncomment php_mysql.dll ext in php.ini and expose "libmysql.dll" file in "path". However this will not work, I am getting the error of: "PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\php\\ext\\php_mysql.dll' - The specified module could not be found.\r\n in Unknown on line 0"

After google for a while I found an excellent solution:


You have to copy "libmysql.dll" file to "windows\system32" directory. Yes, I know, it is already in the Path, you still have to do it. I believe it is related to windows security issue set by MS. Seems this is a very common problem, but the answer is not so obvious from php or mysql website. I update a wiki to post the solution:


Wednesday, July 26, 2006

XML special character

XML understand only thress special characters "& lt;", "& gt;" and "& amp;"
While copying data from html to xml, must be very careful.
Most common error happens for "& nbsp;" which is not allowed in xml/xsl without a declaration.
Fix: change "& nbsp; " to " & #160; " before push it to xml doc.

Also we should change "<" to "& lt;", ">" to "& gt;" before push.
Also note "&" is not allowed for xml doc, however in most HTML pages it has already been changed to "& amp;", which will be valid in XML, so we don't need to take care of this case generally.

Monday, April 17, 2006

A great tip ^M

Today I spent a while fixing the famous ^M problem when copying file from DOS to Unix.

Find two solutions:
1. Use dos2unix command can convert a dos file to unix format (removing annoying ^M)
2. Since originally the dos files are introduced through CVS checkin. If I change the the file type from "binary" to "ASCII" in eclipse. CVS will figure out the file format and automatically remove ^M for me.

One note:
If you open a dos file using vi in Unix environment. vi won't show ^M for you since vi will open the file as dos format. I didn't know this initially.

Thursday, April 06, 2006

Still about oracle time

Oracle time setup is complicated: You have following choices to retrieve timezone and time info:

select to_char(sysdate,'YYYY-MM-DD HH:MI') from dual;
select dbtimezone from dual;
select current_timestamp from dual;
select sessiontimezone from dual;
select systimestamp from dual;

One strange one I encountered today is while I am using the oracle advanced queue (AQ). There are columns named ENQ_TIME and TIME_MANAGER_INFO. Both are autogenerated fields. For ENQ_TIME, oracle use the timezone time (local time); for TIME_MANAGER_INFO, oracle use GMT time! Strange

Monday, April 03, 2006

Oracle reference.

I always forget Oracle's way of retriving the current timestamp.

"select sysdate from dual;"

Next time I can come here to look it up.

Also a very useful oracle reference:


Monday, March 27, 2006

Wasting of time

Last Friday is a bad day for me. I spent three hours in deploying an Eclipse plugin. The plugin was written by another developer. I copied it into my Eclipse plugin directory, everything is fine. Then I made some source code changes and recompiled the package using "javac" and "ant jar" commands. Copied the new jar to plugin directory. It just did not work. Giving me "class not found" exception when Eclipse tried to load the plugin.

AfterI read some online guides about writing Eclipse plugins, I figured out how. Seems I have to create an eclipse plugin project, write the source file, test the plugin using Eclipse runtime test environment, then export the plugin. The magical thing is in the export phase, some meta data is generated for the plugin and saved in a property file, which will be used in plugin deployment. Also I noticed potentially there are two ways of organization of eclipse plugin file, possibly depending on the Eclipse version. I still haven't figured out why my original way of compiling and packaging failed. Because I manually inserted some meta data too.

One lesson, when you are new to something, better stick with a tutorial first. Don't think you know how things work and waste time.

Perforce vs CVS

In my current job, I am required to use Perforce to do version controlling. Perforce is a better versioning tool than CVS, more advanced, complicated and stable. Since I always use Eclipse as my Java IDE, I tried to find a good Perforce plugin for Eclipse. The result is not so good.

I didn't find a tool that can have "Team Synchronization" view like the CVS plugin. So when somebody else editted and checked in (submitted) a file which I didn't open for edit in my client space. I will not know such changes in Perforce. Whereas such change is very easy to view when I do a "Team Synchronization" using default CVS plugin with Eclipse. So now I can only blindly update the files I didn't open for edit in Perforce. I really don't like this idea.

Another thing I don't like is that you have to manually mark a file "Open for add" (Or delete) before you can check in(submit) the change into the depot in Perforce. When you create a file using CVS plugin, a nice commit sign will be posted on the file you just created in the Team Synchronization view, which reminds you this is a new file that should be checked into CVS.

I guess the CVS plugin is so proactive which makes me like CVS, even though I know it's not perforce's fault. I just can't find a tool equivalently powerful as the CVS plugin for Perforce. I have to live with how things work and hopefully I will like it after a while.

Hibernat hates Spring

Hibernate is a cool object-database mapping tool. Spring a J2EE framework. They work together greatly in my previous job. Today I saw an interesting article.


I think the reason is Hibernate don't want Spring to limit its usage. Since it is a product supported by JBoss now, hibernate wants more freedeom. Not only to change its interface easier, but also upgrade itself into a framework tool, which is what Spring has been doing.

Enterprise tools just seem to be growing more and more complex and difficult to use. I hope I can still stick with EJB2 with the help of a nice eclipse plugin named Lomboz. It has some limitations, but very nice after played for a while.

Today, sounds like EJB3(Hibernate3) is the only solution. I have read some tutorial to able to discuss with people about J2EE.

Wednesday, February 08, 2006

The reason to write this blog

I realize it will be a good idea to record my daily programming life. I will be able to see what I did in the past. To see the evolution of the languages and tools I have been using, to see the evolution of my professional life. A little about myself: I am a software engineer, 29 years old, love the programming job I am doing. I have a lovely wife and daughter.