Java JPA operation data, incomplete transaction control clear (), merge () method analysis

Source: Internet
Author: User

A problem occurred when I recently used spring + jpa + cxf to deploy a service!
 
Transaction:
 
Define transaction integrity control in a Service method
For example:
Dao:
 
Public class UserDao {
@ PersistenceContext
Private EntityManager em;
 
Public User saveOrUpdate (User u ){
// Code
Return em. merge (u );
}
 
Public boolean updateUserBySql (String jpa ){
Try {
Em.createquery(sqlcmd.exe cuteUpdate ();
Return true;
} Catch (Exception e ){
Return false;
}
}
 
}
 
Public class UserServiceImpl {
@ Autowired
Private UserDao userDao;
@ Autowired
Private TransactionService tranService;
 
// Obtain the basic configuration information of the cache in the middle (loading the information in the configuration table every half an hour)
@ Autowired
Private CommonService commonService;
 
Public Aresp updateUserTradeOrders (Areq req ){
TransactionStatus status = tranService. getTransactionStatus ();
Try {
User u = XXXX (req); // converts a req object to a user object.
String jpa = "update user SQL"; // changes all the statuses of the user to valid to invalid SQL
Boolean bool = userDao. updateUserBySql (jpa );
 
If (bool ){
UserDao. saveOrUpdate (u)
}
Aresp a = Aresp. createSuccess ();
A. setDesc (commonService. getSysconfig (a. getCode ));
TransService. commit (status );
Return;
} Catch (Exception e ){
E. pr;
TransService. rollback (status );
Return Aresp. createFaild ();
}
}
}
 
In CommonService, the method is to perform a refluse every 30 minutes. In refluse, the clear () call is performed on em,
 
The method interface is provided, and there is no problem in testing! Later, during the test, it was suddenly found that the user information updated once every half an hour was invalid, but the newly added entity was not saved. There was a question:
1. Although my code is unreasonable, the theory should be that commit is getting the descriptive language first, but after I call clear (), there is no previous valid state changed to invalid
2. Why can't I keep everything clear without changing my data? Half of the execution destroys the integrity of the transaction. The original intention of using transaction control is as follows:
 
View the usage and practice of jpa's clear () method: the clear () method separates all entities currently being managed
Em. clear () program all entity objects in the Entity Manager in a free state.
Merge synchronizes entities with databases.
Through two points of analysis, if. First merge data in a non-existent database, and then clear objects in commit will not be saved to the database. If this record exists at the beginning of the database, it will be updated after commit

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.