A solution to the data not being updated in time when DAO is called in spring _java

Source: Internet
Author: User

Describe a few prerequisites before describing the problem, assuming that the transaction is configured in the following way in the spring configuration file:

 <bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
   <property name= "DataSource" ref= "DataSource"/>
 </bean> 
 <tx:annotation-driven transaction-manager= "TransactionManager"/>

Now there are Userdao and Securityservice:

@Repository public
 class Userdao {public
   user GetUser () {
     //query user ' from user table return
     QUERYOBJEC T ("SELECT * from user order BY id DESC limit 1");
   }
 
 @Service @Transactional public class Securityservice {@Autowired private Userda
 
   o Userdao;
       public void Checkuserinfo () {while (true) {User user = Userdao.getuser ();
         if (user!= null && "Tom". Equals (User.getname ()) {System.out.println ("Tom is Here");
       Break }
     }
   }
 }

The data obtained through the Userdao#getuser () method is invariant during the call to the Securityservice#checkuserinfo () method, even though a new data loop with the name Tom is inserted at this time will not end. In addition, it will not help to remove the @transactional annotations above Securityservice. The
first thought about whether it would be a database connection pool, and that was the case with spring itself, and then the connection object was called directly from the JdbcTemplate, and the original JDBC was used to manipulate the database, and the data changed in real time, It was then thought that the transaction of spring should be bound with the current operation thread. Look at the source code. Spring was found in the Datasourceutils#dogetconnection method. A connection is created on each datasource of each thread and is bound to a transaction. Because the Tx:annotation-driven configuration file is bound to all service tiers (classes with @service annotations), the same connection is bound in the same thread regardless of whether or not @transactional is used. It's just not a transactional operation.
After many experiments and search data, finally found the perfect solution: As long as the above Checkuserinfo method to add @Transactional (propagation = propagation.not_supported) Annotations on it. Of course, you can get to connection and then do it manually, or you can use the Dateutils package to do the operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.