Rule Definition
session.setFlushMode(FlushMode.COMMIT, FlushMode.NEVER or FlushMode.MANUAL) specifie that the session won't be flushed before query execution (it will be flushed only at the end of the database transaction). Be aware that this setting may expose you to stale data: modifications you made to objects only in memory may conflict with the results of the query. We don't recommend that you change this setting from the default.
It's provided to allow performance optimization in rare cases. Likewise, most applications rarely need to call flush() explicitly. This functionality is useful when you're working with triggers, mixing Hibernate with direct JDBC, or working with buggy JDBC drivers. You should be aware of the option but not necessarily look out for use cases.
Remediation
Verify that these settings are necessary.
Violation Code Sample
case#1: public void testJEEPPUB003_1_SHOW_2() { UsersFromVb newUser = new UsersFromVb("Jake2","Roberts",23,"Chimp@EroticMonkey.com"); UsersFromVbHome userManager = new UsersFromVbHome(); //userManager.persist(newUser); Session mySession = userManager.getSessionFactory().getCurrentSession(); mySession.getTransaction().begin(); //userManager.attachClean(newUser); mySession.persist(newUser); mySession.setFlushMode(FlushMode.COMMIT); //VIOLATION //userManager.persist(newUser); mySession.getTransaction().commit(); } case#2: public void testJEEPPUB003_1_SHOW_4() { UsersFromVb newUser = new UsersFromVb("Jake2","Roberts",23,"Chimp@EroticMonkey.com"); UsersFromVbHome userManager = new UsersFromVbHome(); //userManager.persist(newUser); Session mySession = userManager.getSessionFactory().getCurrentSession(); mySession.getTransaction().begin(); //userManager.attachClean(newUser); mySession.persist(newUser); mySession.setFlushMode(FlushMode.MANUAL); //VIOLATION //userManager.persist(newUser); mySession.getTransaction().commit(); }
Fixed Code Sample
public void testJEEPPUB003_1_SHOW_4() { UsersFromVb newUser = new UsersFromVb("Jake2","Roberts",23,"Chimp@EroticMonkey.com"); UsersFromVbHome userManager = new UsersFromVbHome(); //userManager.persist(newUser); Session mySession = userManager.getSessionFactory().getCurrentSession(); mySession.getTransaction().begin(); //userManager.attachClean(newUser); mySession.persist(newUser); //userManager.persist(newUser); mySession.getTransaction().commit(); }
Reference
Hibernate in Action (ISBN 1932394-15-X) p 160
Related Technologies
JEE
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.