I have a scenario service layer is transactional, where i can only commit after done trandaction. i simplified it into like below.
begin transaction
for(loop){
getHibernateTemplate().save(object);
getHibernateTemplate().get(object_by_key); //cannot get object by object_by_key because "object" is not commit into database until entire for(loop) completed.
}
end transaction. commit();
i try to put getHibernateTemplate().flush(), after save() and able to see “insert” in show_sql. but the record is not showing inside database. how to force write to database after each save() rather than wait for commit like above ?
getHibernateTemplate().flush() is the method to force hibernate to write to the database (send insert & update queries). This is done within a transaction so it is not visible to other transactions (querying from a SQL client) till the transaction is committed.
If the insert query is showing up in the log then it has been sent to the database. If you want to test that the record was inserted correctly – you can either do a getHibernateTemplate().clear() (which will remove all the cached data) and then do a getHibernateTemplate.get() (which will query from the dataSource). Or the other approach to test is to use the jdbcTemplate (with the same database) to query and check.
If the SQL client tool you are using allows you to specify the Isolation level – starting the SQL client session in isolation read_uncommited – would allow you to see the changes done even before the transaction is commited.