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.