Five ‘s Weblog

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.

10_29_2_2007.jpg

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

        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!

10_29_1_20071.jpg

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.

10_25_1_2007.jpg

       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:

<web-app>
         /*the listener watches over the creation of ServletContext which store the spring applicationContext*/
        <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
 
        <context-param>/*list the spring configuration files*/
             <param-name>contextConfigLocation</param-name>
             <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>

        <filter>
               <filter-name>OpenSessionInViewFilter</filter-name>
               <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
               <init-param>
                        <param-name>sessionFactoryBeanName</param-name>
                        <param-value>sessionFactory</param-value>
              <init-param>
       </filter>

       <filter-mapping>
               <filter-name>OpenSessionInViewFilter</filter-name>
               <url-pattern>/*</url-pattern>
       </filter-mapping>

       ……..
</web-app>

10_25_2_2007.jpg

Create a free website or blog at WordPress.com.