Five ‘s Weblog

November 2, 2007

Transaction Concurrency (1)

Filed under: Five's thought — by powerdream5 @ 10:07 pm
Tags: , , ,

       One of the big differences between Enterprise Application and Normal web application is that the enterprise applications must handle the problem of concurrency, because they are usually multiuser applications, where several transactions update the database at the same time frequently. Before we can deal with concurrency, It is useful to know more about it.


       In fact, we devide the problems caused by concurrency into several categories. They are:
        Lost Update : Two transactions both update a row and then the second transaction aborts, causing both changes to be lost.
       Dirty read : One transaction reads changes made by another transaction that hasn’t yet been committed. However, if the second transction roll back, the data the first transaction readed is not correct!
       Unrepeatable read : A transaction reads a row twice and reads different state each time. For example, another transaction may have written to the row, and committed, between the two reads.
       Second lost updates problem : A special case of an unrepeatable read. Imagine that two concurrent transactions both update arow, one writes to it and commits, and then the second writes to it and commits. The changes made by the first writer are lost. This situation is pretty similar to Lost Update, please note the differences between them.
      phantom read : A transaction executes a query twice, and the second result set includes rows that weren’t visible in the firstresult set. This situation is caused by another transactioin inserting new rows between the executions of the two queries.

     Ok, It is time to handle concurrency now. Usually, it is not a good way to serialize transactions which means make the transactions execute one by one and it will reduce the performance of the application. In fact, we define severl transaction isolation levels. They are separately:
     Read Committed : Permits unrepeatable reads but not dirty reads. Reading transactions don’t block other transactions from accessing a row. However, an uncommitted writting transaction blocks all other transactions from accessing the row.
     Repeatable read : permits neither unrepeatable reads not dirty reads. Phantom reads may occur. Reading transactions block writing transactions (but not other reading transactions), and writing transactions block all other transactions.

     In Hibernate framework, we can set the isolation for JDBC connections using a Hibernate configuration option. The defaultisolation level is repeatable read isolation whichs is represented by number 4 (Hibernate.connection.isolation = 4). 1 means read uncommitted isolation; 2 means read committed isolation; 8 means serializable isolation. In most cases, we prefer 2.

     However, it is not a good idea to let the database to deal with the concurrency by itself, we think it is the responsibility of the application to handle them. In my next article, I would like to write something about how to deal with concurrency using optimistic locking and pessimistic locking.


October 30, 2007

Is there any repeat?

Filed under: Five's thought,Spring — by powerdream5 @ 9:41 pm
Tags: ,

       As we all know, Spring DAO is based on template method pattern. The reason why Spring DAO adopts template method pattern is that there are some setted processes before and after accessing the database, such as start and commit the transaction, open and close the database connection. Therefore, when we use Spring DAO template, for example, HibernateTemplate and JdoTemplate, we don’t need to write code for transactions by ourselves, since the DAO template will does that job for us.

       However, I has been thinking about a question , and I still not find the answer to it. Spring framework supports declaration transaction. In fact, in many projects, we use DAO template to access the database, at the same time, we also configure transaciton in spring configuration files. Therefore, is it necessary to declare transaions in spring configuration files when we already take the advantage of DAO template, which already helps us handle transactions? is it redundant for us to do that?

       In order to find the answer, I have refered to some materies. There is an explansion in spring document. It is that “even if HibernateTransactionManager is used for transaction demarcation in higher-level services, all those services above the data access layer don’t need to be Hibernate-aware. Setting such a special PlatformTransactionManager is a configuration issue: For example, switching to JTA is just a matter of Spring configuration (use JtaTransactionManager instead) that does not affect application code.”

      What is your opinion about this explanation. To be honest, It doesn’t make sense to me and it still not help me answer this question. I am sorry for my slow response. I hope you can help me solve this question! Thanks a lot!


October 29, 2007

Autowire in Spring

Filed under: Spring — by powerdream5 @ 8:32 pm
Tags: , , , ,

         At the beginning, let’s talk about the function of autowire in spring briefly. In order to save the programmers’ time and effort in writing the spring configuration files by hand, Spring framework introduces the autowire function. To be honest, this function is pretty useful sometimes, but I don’t recommend to use it, since it always bring some problems confusing you. In the following, I would like to show you an example to exmplain why I avert autowire.


        <beans default-autowire=”autodetect”> <!–autowire is the cause of exception–>
                <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”>
                <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
               <bean id=”cronReportTrigger” class=”org.springframework.scheduling.quartz.CronTriggerBean”>
               <bean id=”schedulerFactoryBean” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>
                         <property name=”triggers”>
                                       <ref bean=”cronReportTrigger”/>

        In my example, firstly, I configure Hibernate. In order to execute a method in a specific time, I configure Quartz. Both of the two configuration processes are so easy that you will not come across any obstacles. However, if you set the autowire properties, when you start the server, you would get the exception: “Failure obtaining db row lock: Table ‘hibernate.qrtz_locks’ doesn’t exist“. I believe you must be confused, and you would ask why the SchedulerFactoryBean class has relationship with Hibernate.

        In fact, this exception is caused by the autowire property of Spring. The org.springframework.scheduling.quartz.SchedulerFactoryBean has a method named setDataSource. Because we set the autowire properiy as “autodetect”, and we also configure database source in our application, the spring container will inject this database source into the SchedulerFactoryBean class automatically, then SchedulerFactoryBean class will looking the database for tasks. There is no task in your database, therefore, you get the exception. Fortunately, as soon as you realize the cause of the exception, it is easy to handle it by removing the autowire property. Then, restart your server, things goes well.

         From this example, we can see that the autowire property may bring some problems over our expectation. Therefore, use this property cautiously!


October 28, 2007

Sending Email with Spring mail abstraction layer

Filed under: Spring — by powerdream5 @ 5:45 pm
Tags: , ,


        Spring mail abstraction layer is based on javaMail. In fact, there are already a lot of examples in the internet to show how to send Email with spring mail abstraction layer, and they are easy to understand. Therefore, in this article, I will not do some repeated work, and just want to show some problems which you may come accross when your are programming.

        If you have your own mail server. It is very easy to write a email program, since you will not meet some “confusing problems”. If you not have one, this article is very useful for you. I still remembered how frustrated I was the first time when I coded a program for sending email with spring mail abstration layer through a public mail server(such as yahoo, gmail).

       Firstly, make sure you can connect to the mail server. Using telnet tool is very helpful for us to judge if we can connect it. Furthermore, we also need the username and password to log in. However, sometimes even you provide the usename and password, perhaps you still get the javax.mail.authenticationfailedexception. This exception means you cannot access this mail server from the traditional E-mail Clients, like foxmail and outlook, even you can telnet it (for example, and The only way to handle this problem is changing your email server.

       Second, make sure your email server allows you to send email from the traditional E-mail Clients. Otherwise you will get the exception: 533 this mail is blocked by kbas system. KBAS system is anti-spam software. Because it is installed in your email server, and this software will recognize all the emails sent from the traditional E-mail Clients as spam so that it will prevent you from sending them. (For example,

       I have tried many email server, and found gmail is the one I really need. You will not meet the first two abstracles I listed above with gmail. In the following, I would like to show the sping configruration file.

       <bean id=”mailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>
               <property name=”host”>
               <property name=”javaMailProperties”>
                              <prop key=”mail.smtp.auth”>true</prop>
                              <!–this is important, otherwise you will get the
                                     exception: 530 5.7.0 Must issue a STARTTLS command
                              <prop key=”mail.smtp.starttls.enable”>true</prop>
                              <prop key=”mail.smtp.timeout”>25000</prop>
                <property name=”username”>
                <property name=”password”>
                <!–you don’t need to set the port number, 25 is default–>


October 25, 2007

Exposed Domain Model Pattern

Filed under: Five's thought — by powerdream5 @ 11:10 pm
Tags: , , , ,

       In my former articles, I have mentioned “open session in view” many times. What is open session in view? When we use Hibernate framework, the Hibernate session will be closed after a transaction being submitted. Then, when the presenter layer of a web application accesses the detached objects, it may access an unloaded object because of the mechanism of lazy loading. Therefore, an exception would thrown.


       One of the good way to handle this problem is using Exposed Domain Model Pattern instead of POJO Facade Pattern. We also call Exposed Domain Model pattern as Open Session in View Pattern. It means the presenter layer can access the business logic layer directly instead of through a facade class.  It is obvious that the advantages of POJO Facade pattern are the disadvantages of Exposed Domain Model. However, If you want to avert the complexity of writting the detached objects, in fact, which are also weak,  Exposed Domain Model pattern is still a good choice.

       In POJO Facade, the facade class need to take care of managing transaction and database connection. In Exposed Domain Model, we have to distribute the two functions to the other parts of the system. Because we have to keep the Hibernate session availabe until the presenter layer finishes rendering the page, the servlet filter is a good place to open and close a session. Spring framework provides such a filter named “OpenSessionInViewFilter” to help us realize this.  Furthermore, we can also start and submit a transaction in the servlet filter. However, it is too complicate to write code to restart a transaction in the servlet filter. Usually, we let the classes in the business layer (for example, the service classes in the domain model) to deal with the transaction.

The following is an example of web.xml file to show how to config spring framework in web.xml:

         /*the listener watches over the creation of ServletContext which store the spring applicationContext*/
        <context-param>/*list the spring configuration files*/





October 24, 2007


Filed under: Programming — by powerdream5 @ 10:53 pm
Tags: , ,


       Today, I found a userful utility classes package when I read my former notes. It is org.apache.commons.beanutils, one of the packages of Jakarta commons. We call the org.apache.commons.beanutils package Bean Introspection Utilities. When we use setter to initialize the properties of a Javabean, especially which has a lot of properties, we always complain that there are many setter methods needed to write. Fortunately, with the help of the org.apache.commons.beanutils, we can just use one sentence to realize that. The following is an example: (you need to download the package firstly)

// a simple javabean
public class User{
        private String username;
        private String password;
        private int age;

        //the setter, getter methods
        public String getUsername(){
               return username;
        public void setUsername(String username){
              this.username = username;
       …omit to save space …

import java.util.HashMap;
//import the necessary class
import org.apache.commons.beanutils.BeanUtils;

public class Test{
      public static void main(String[] args){

              //in order to show this example, I create a map
             //if this class is a servelet, we can use request.getParameterMap()
             //get all the properties of the form stored in a map

             HashMap map = new HashMap();
            User user = new User();
            }catch(Exception e){


       populate(java.lang.object bean, java.util.Map properties) is a static method. This method can do some basic type conversions such as from String to int. However, if there are a mismatch with the type, and cannot be converted, an exception will be thrown.


       Using this package relly makes things easy. However, some people point that the performance of this package is not good, whichis not as good as using the setter method directly. In fact, we can find the factors which influence the performance. The org.apache.commons.beanutils package is based on the mechanism of reflection. Therefore, before we can assign value to the properties, we have to introspect the bean class to find proper properties, then invoke the setter methods. Compared with using setter methods directly, this method really take a detour. However, if you don’t care a little loss in the performance of your application, and want to save some effort, using this package does no harm!


October 23, 2007

POJO Facade

Filed under: Five's thought — by powerdream5 @ 9:23 pm
Tags: , ,

      The day before yesterday, I made a brief introduction to the Facade pattern. Today, let’s make a little bit advance. Because I am studying lightweight frameworks now, I am pretty willing to share my own knowledge about POJO facade pattern with you!


      From the name of POJO facade, it is easy to tell that it is based on the facade pattern. The main responsibility of POJO facade is to manage the transaction and database connection. In fact, when we refer to POJO facade, we always like to compare it with Session facade. They have two big differences. The first one is POJO facade donesn’t need the services provided by the EJB Server. Because POJO facade pattern is specific for lightweight framework, such as Spring and Acegi Security, which will provide services to it instead of EJB Server. The second difference is that POJO facade choose to return the domain object to the present layer instead of DTO(Data Transfer object). As we all know, DTO just holds data and not has behaviros. The biggest disadvantage of DTO is that we always need to write a lot repeated and redundant codes to create it.


      As mentioned above, POJO facade returns domain objects to the present layer instead of DTO. However, this way has one drawback that the present layer may call the mehtods of the domain objects directly instead of through POJO facade, so that these calls will not be executed in transaction. In order to solve this problem, we can combine POJO facade with Adapter pattern. Firstly, we define an interface, which contains all the methods the present layer need access. Then, we should create an adapter which implements this interface. And this adapter will transfer all the requests from the client to the domain objects.

     When we use POJO facade, another problem we need to concern is that how the POJO facade returns the domain objects to the present layer(we call it detach). Because of the problem of open session in view. We have to make sure the domain objects returned by POJO facade are the ones needed by the present layer, and the present layer will not access the other domain objects which have not beed loaded from the database.


October 21, 2007

Facade Pattern

Filed under: Five's thought — by powerdream5 @ 9:39 pm

       In EJB, we recommend to use session facade pattern to let the present layer access the business layer. But in lightweight framework, we use POJO facade instead of session facade. There is no doubt that POJO facade is different from session facade. However, the common characteristic between them is  that both session facade and POJO facade are based on the facade pattern. So today I am going to make a brief introduction to facade pattern according to my own understanding.


       To be honest, compared with the other design patterns, facade pattern is easier to understand in my opinions. The general purpose of using facade pattern is to reduce the complexity of the system. The basic concept of facade pattern is to provide a simple interface to a complex subsystem, and the clients of the subsystem must access it through this interface, thereby promoting subsystem independence and portability.

       People prefer to taking the car as an example to explain the facade pattern. If we compare the car to the system, the driver will be the client of the system and the steering wheel will be the facade interface. In order to drive the car, the driver just need to interact with the sterring wheel. He doesn’t need to knwo how the engine works and how the tires turn.

//the subsystem 
public class Engine{
     public void works(){…}

public class Tire{
      public void turn(){…}

//the interface
public class SterringWheel{
       private Engine engine;
       private Tire tire;
      //setter injection
      public void set(Engine engine){
           this.engine = engine;
      public void set(Tire tire){
           this.tire = tire;

      //encapsulate the detail of the subsystem
      public void driven(){

//the client
public class Driver{
     private SterringWheel sterringWheel;
     public void setSterringWheel(SterringWheel sterringWheel)
              this.sterringWheel = sterringWheel;
    public void drive(){


October 19, 2007

What is real object-oriented?

Filed under: Five's thought — by powerdream5 @ 9:19 pm
Tags: , ,

       From the first time I touched C++ (although C++ is not considered a pure OOP language), I have studied OOP language for more than 4 years. But one question has beed confusing me. What is real object-oriented? Sometimes, it seems that I get the answer, but sometiems, I lost myself again.


     When I started learning OOP language, I compared it with C. Because OOP language has classes and C dones’t have. Therefore, I thought that this character is the biggest difference which can help me distinguish OOP language and NO-OOP language. It is obvious that I made a mistake. I just focused on the differences of appearance between OOP and NO-OOP. In fact, there are a lot of programmer who code in procedure-oriented way when they are using OOP language. I don’t want to deny I am one of them.

       Along with getting more knowledge about OOP, I had to change my opinions about it. It is well known that inheritence, polymorphism and encapsulation are the basics of the OOP. Design patterns take fully adavantage of these merits of OOP language. Therefore, I thought the essence of OOP is applying the characteristics to programming(designing and coding).

       I believed I was right until I heard about anemia model. We can use all kinds of design patterns to realize the business logic, but the persistence classes, which are pojo(plain and old java object), just have a series of properties and pairs of gettter and setter method. According to Martin Flower, Anemia model is based on transaction script(procedure-oriented), because we separate the behaviors and properties of a object. In contrast, we need to combine the behaviors and properties of a object into a class in the OOP world.

      Therefore, I still have no answer to the question that what is real object_oriented. Perhaps I will not get the answer until I stop programming, because I don’t care about the answer any more at that time!


October 18, 2007

uni-directional or bidirectional ?

Filed under: ORM — by powerdream5 @ 10:52 pm
Tags: , , , ,

        When mapping the domain model to the database schema,  perhaps we often ask ourselves one question: Making a one-to-many relationship uni-directional or making it bidirectional, which is better in performance. Today, I am going to show an example to demonstrate the difference between uni-direction and bidirection. I am using Hibernate framework.

        There is a class named Consumer and a class named CreditAccount, both of which mapped to the consumer and credit_account table respectively. It is obvious that each Consumer has more than one credit account, therefore, the relationship between the two classes is one-to-many. Firstly, I make the relationship uni-directional.


the mapping files are listed in the following:

          <class name=”Consumer” table=”consumer”>
                 <id name=”id” column=”CONSUMER_ID”>
                       <generator class=”native” />
               <property name=”firstName” type=”string” column=”FIRSTNAME”/>
               <property name=”lastName” type=”string” column=”LASTNAME”/>
               <set name=”creditAccount” table=”credit_account” cascade=”all”>
                      <key column=”CONSUMER_ID”/>
                      <one-to-many class=”CreditAccount” />

          <class name=”CreditAccount” table=”credit_account”>
                 <id name=”id” column=”ACCOUNT_ID”>
                         <generator class=”native” />
                 <property name=”accountNumber” type=”string” column=”ACCOUNT_NUMBER”/>

We try to insert an new account into the database, from the comman window or log files, we can see that two sql setences are executed. There are:
insert into credit_account(ACCOUNT_NUMBER) values(?)
update credit_account set CONSUMER_ID=? where ACCOUNT_ID=?

Now, I am going to make a little change to the CreditAccount class to make the relationship between Consumer and it bidirectional.  Therefore, I also need to change the mapping files. After being changed, they should be:

          <class name=”Consumer” table=”consumer”>
                 <id name=”id” column=”CONSUMER_ID”>
                       <generator class=”native” />
               <property name=”firstName” type=”string” column=”FIRSTNAME”/>
               <property name=”lastName” type=”string” column=”LASTNAME”/>

               //make sure to set “inverse=true”
               <set name=”creditAccount” table=”credit_account” cascade=”all” inverse=”true”>
                      <key column=”CONSUMER_ID”/>
                      <one-to-many class=”CreditAccount” />

          <class name=”CreditAccount” table=”credit_account”>
                 <id name=”id” column=”ACCOUNT_ID”>
                         <generator class=”native” />
                 <property name=”accountNumber” type=”string” column=”ACCOUNT_NUMBER”/>
                 <many-to-one name=”consumer” class=”Consumer” column=”CONSUMER_ID”>

Now, if we insert a new account into the database, only one sql sentence will be executed:
insert into credit_account(ACCOUNT_NUMBER,CONSUMER_ID) values(?, ?)

       There is no doubt that bidirection is better than uni-direction in the performance. If the relationship is uni-direction, only one end is going to maintain their relationshop. The other end cannot see any change of the relationship.  Therefore, the first time we need to execute two sql sentences, the first sql sentence inserts the data into the credit_account table (at this time, the system doesn’t know this account belongs to whom), and the second sql help the credit_account table build its relationship with the consumer table. However, both the end of the bidirectional relationship are aware of any changes, therefore, only one sql sentence can finish two tasks: inserting data and mataining the relationship.

        You can download the source files.


« Previous PageNext Page »

Blog at