Hibernate does not lock objects in memory. Your application can expect the behavior as defined by the isolation level of your database transactions.
Note that thanks to the Session, which is also a transaction-scoped cache, Hibernate provides repeatable reads for lookup by identifier and entity queries (not reporting queries that return scalar values).
Hibernate 沒有提供在內(nèi)存中對(duì)對(duì)象加鎖的功能,因此應(yīng)用的事務(wù)隔離級(jí)別是完全由數(shù)據(jù)庫(kù)來提供的。
借助于session,Hibernate 提供了可重復(fù)讀的事務(wù)隔離級(jí)別,但是前提是只有使用identifier and entity queries的可以,使用標(biāo)量查詢則不可以,其原因是hibernate的cache的對(duì)象需要是能夠被唯一識(shí)別的實(shí)體對(duì)象,標(biāo)量查詢返回的結(jié)果是數(shù)組,不滿足該要求,無法在session級(jí)別進(jìn)行事務(wù)的cache
在事務(wù)和并發(fā)性方面,hibernate不僅提供了基于版本的控制的自動(dòng)樂觀鎖機(jī)制,同時(shí)還提供了API來實(shí)現(xiàn)悲觀鎖機(jī)制(通過SELECT FOR UPDATE 語(yǔ)法)
A SessionFactory is an expensive-to-create, threadsafe object intended to be shared by all application threads. It is created once, usually on application startup, from a Configuration instance.
A Session is an inexpensive, non-threadsafe object that should be used once, for a single request, a conversation, single unit of work, and then discarded. A Session will not obtain a JDBC Connection (or a Datasource) unless it is needed, hence consume no resources until used.
Database transactions are never optional, all communication with a database has to occur inside a transaction, no matter if you read or write data.
事務(wù)并不是可有可無的,所有的數(shù)據(jù)庫(kù)操作都是在事務(wù)環(huán)境下發(fā)生的,不管是read 操作還是write操作。這點(diǎn)一定要名明確,以前曾經(jīng)誤解。
As explained, auto-commit behavior for reading data should be avoided, as many small transactions are unlikely to perform better than one clearly defined unit of work. The latter is also much more maintainable and extensible.
最好關(guān)閉自動(dòng)提交功能,因?yàn)槭褂么罅康亩虜?shù)據(jù)庫(kù)事務(wù)在性能上未必比一個(gè)清晰定義的unit work強(qiáng),后者更加易于維護(hù)和擴(kuò)展。(以前曾經(jīng)誤解)
樂觀鎖的處理方式
Hibernate checks instance versions at flush time, throwing an exception if concurrent modification is detected.It's up to the developer to catch and handle this exception (common options are the opportunity for the user to merge changes or to restart the business conversation with non-stale data).
在flush的時(shí)候檢查version,如果發(fā)現(xiàn)更改會(huì)拋出異常,開發(fā)人員可以捕捉該異常,并決定如何處理(要么合并修改,要么放棄修改)
悲觀鎖的處理方式
悲觀鎖基本上是靠數(shù)據(jù)庫(kù)本身提供的機(jī)制來實(shí)現(xiàn),通常只要為jdbc connection指定事務(wù)隔離的級(jí)別即可。The LockMode class defines the different lock levels that may be acquired by Hibernate.
悲觀鎖會(huì)在讀取數(shù)據(jù)之前獲取,因此能夠保證當(dāng)前事務(wù)的數(shù)據(jù)就是最新數(shù)據(jù)。
hibernate的查詢分類:
1、標(biāo)量查詢(Scalar queries):返回的記錄數(shù)組的集合
標(biāo)量是與數(shù)組相對(duì)的概念,是一種由單一的值來表征的量。所謂標(biāo)量查詢,就是直接使用原生sql語(yǔ)句的查詢,也即返回的是裸數(shù)據(jù)(只不過hibernate使用ResultSetMetaData將resultset中的每一條記錄放到一個(gè)數(shù)組Object[]中
)。
比如
List list = session.createSQLQuery("select * from t_area_person").list();
for (Object object : list) {
Object[] ob = (Object[]) object;
for (int i = 0; i < ob.length; i++)
System.out.print(" " +ob[i]);
System.out.println();
}
2、實(shí)體查詢(Entity queries):返回的是實(shí)體對(duì)象的集合
3、Load方法:lookup by identifier ,返回的是該identifier 對(duì)應(yīng)的對(duì)象