Five ‘s Weblog

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!

10_30_1_2007.jpg

Advertisements

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 28, 2007

Sending Email with Spring mail abstraction layer

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

10_28_1_2007.jpg

        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, smtp.163.com and smtp.126.com). 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, smtp.sohu.com)

       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”>
                      <value>smtp.gmail.com</value>
               </property>
               <property name=”javaMailProperties”>
                      <props>
                              <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>
                    </props>
                </property>
                <property name=”username”>
                       <value>caow190@gmail.com</value>
                </property>
                <property name=”password”>
                       <value>password</value>
                </property>
                <!–you don’t need to set the port number, 25 is default–>
         </bean>

 10_28_2_20071.jpg

Blog at WordPress.com.