Transaction续一
(Resent) The mail topic "share some knowledge based on Felcra project"
1) About transaction
Inside transaction logic:
Never use: try{}catch(Exception e) {e.printStackTrace();};
Never use: try{}catch(Exception e) {};
Never use: try{}catch(Exception e) {log.....};
but use: try{
//biz logic
} catch(Exception e) {
//can print //can log
throw e; //very very important
};
Spring can define the rollback transaction exception type, but if the defined exception is not thrown from the catch-block, the whole transaction will not be rolled back. This point is very very important.
2) About Error thrown.
Try not to do biz logic at the catch-block, except throws exception directly.
But sometime need to write log or db, which is special case.
Do less thing at the catch-block, especially never include any business logic.
If some biz logic is necessary, try to think use Boolean-Return or State-Pattern or Object-Return to replace.
The reason? Because biz logic in catch-block will cost a lot of JVM resource and it will also cause performance problem, this is traps in JAVA.
(Resent) The mail topic "knowledage for ost-dao-config.xml"
3) About ost-dao-config.xml.
There are 2 interceptors inside "ost-dao-config.xml", which is quite different from SD FFB Book.
SD FFB only use Hibernate, whose scope includes "*Service" "HibernateDao"
FC Raw use both Hibernate, Spring JDBC, also JMS.
If only Hibernate, OpenSessionInView Pattern will manage the transaction, which should transacted SessionFactory.
If not only Hiberante, SessionFactory which is transaction-wrapped can only manage Hibernate. Spring JDBC/JMS is out of the SessionFactory scope.
Now come to explain the 2 interceptors:
1) transactionInterceptor: It manages beans with name: <value>*dbcTemplate,*Service,*Sender,mhlImportBean</value>. It manages all except Hibernate-related.
2) hibernateInterceptor: It manages bean: sessionFactory, which will transaction-wrapped Hiberante-related. If hibernateInterceptor is not configured, the evil thing will happen: HibernateDao will write into DB while error thrown in *Service.
4) How does the 2 methods in Hibernate get data?
HibernateDao.getObject(), which used a lot in FC Raw/SD FFB Book.
In fact, it uses Spring HibernateTemplate.get(); which will get data from Hibernate 1st-level cache first, if get, then return; Otherwise, get from Database.
Another one is Criteria.list(); The method will get data from database as queried.
5) HibernateDao.flush()
It is out-of any transaction-rollback scope. Should Never or less use.