What is hibernate session flush )? You may not be familiar with this, but I believe that you will have a great benefit after reading this article.
Each interval for a period of time,Session will execute some necessary SQL statements to synchronize the status of objects in the memory to the JDBC connection. This process is called the hibernate session flush ).
It will be executed at the following time points by default:
◆ Before some queries are executed
◆ When org. hibernate. transaction. Commit () is called
◆ When session. Flush () is called
◆ The SQL statements involved are executed in the following order:
◆ All statements for inserting objects are executed in the time sequence of session. Save ().
◆ All statements for updating objects
◆ All statements for deleting a set
◆ All statements for deleting, updating, or inserting collection Elements
◆ All statements for set insertion
◆ All statements for deleting objects are executed in the order of session. Delete () time.
(One exception is that if an object uses the native method to generate IDS (persistence identifiers), they will be inserted as soon as they are executed .)
Unless you explicitly issue the flush () command, it is completely uncertain about when the session will execute these JDBC calls. They can only ensure the execution order.Of course, Hibernate guarantees that query. List (...) will never return invalid data or error data.
You can also change the default settings to make the hibernate session flush operations happen less frequently. The flushmode class defines three different methods. Flush () is only available at the time of submission (only valid when hibernate's transaction API is used). It is flushed out according to the method just mentioned, and never flush () unless explicitly used. The last mode is useful for long-running work units that need to keep the session open or disconnected for a long time.
Sess = SF. opensession ();
Transaction Tx = sess. begintransaction ();
// Allow queries to return stale state
Sess. setflushmode (flushmode. Commit );
Cat Izi = (CAT) sess. Load (Cat. Class, ID );
Izi. setname (iznizi );
// Might return stale data
Sess. Find ("from cat as cat left Outer Join cat. Kittens kitten ");
// Change to Izi is not flushed!
TX. Commit (); // flush occurs
Sess. Close ();
During the flushing of hibernate sessions (flush), an exception may be thrown. (For example, a DML operation violates the constraints) exception handling involves understanding hibernate transaction behavior. Here we will not introduce the transaction and concurrency here.
Another case is worth noting: when a large amount of data is inserted
For example, I want to insert 10000 users now. What should I do?
Many may think like this:
Transaction Tx = session. begintransaction ();
For (INT I = 0; I <10000; I ++)
{
User u = new user (......);
Session. Save (U );
}
TX. Commit ();
Session. Close ();
However, as the program runs, it always fails at a certain point in time. An outofmemoryexception (memory overflow) is thrown!
This is because the hibernate session holds a required level-1 cache, and all user instances will be cached in the session-level cache area. However, the cache area cannot be installed ".
How can this problem be solved?
Solution: regularly cache sessions into the database!
For (INT I = 0; I <10000; I ++)
{
User u = new user (......);
Session. Save (U );
For (I = 0)
{
Session. Flush ();
Session. Close ();
}
}
TX. Commit ();
Session. Close ();
Reference: http://developer.51cto.com/art/200909/153860.htm