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 没有æä¾›åœ¨å†…å˜ä¸å¯¹å¯¹è±¡åŠ é”的功能åQŒå› æ¤åº”用的事务隔离¾U§åˆ«æ˜¯å®Œå…¨ç”±æ•°æ®åº“æ¥æä¾›çš„ã€?/strong>
借助于sessionåQŒHibernate æä¾›äº†å¯é‡å¤è¯Èš„事务隔离¾U§åˆ«åQŒä½†æ˜¯å‰ææ˜¯åªæœ‰ä½¿ç”¨identifier and entity queriesçš„å¯ä»¥ï¼Œä½¿ç”¨æ ‡é‡æŸ¥è¯¢åˆ™ä¸å¯ä»¥åQŒå…¶åŽŸå› æ˜¯hibernateçš„cacheçš„å¯¹è±¡éœ€è¦æ˜¯èƒ½å¤Ÿè¢«å”¯ä¸€è¯†åˆ«çš„å®žä½“å¯¹è±¡ï¼Œæ ‡é‡æŸ¥è¯¢˜q”回的结果是数组åQŒä¸æ»¡èƒöè¯¥è¦æ±‚ï¼Œæ— æ³•åœ¨session¾U§åˆ«˜q›è¡Œäº‹åŠ¡çš„cache
在事务和òq¶å‘性方é¢ï¼Œhibernateä¸ä»…æä¾›äº†åŸºäºŽç‰ˆæœ¬çš„æŽ§åˆ¶çš„自动ä¹è§‚锿œºåˆ¶åQŒåŒæ—¶è¿˜æä¾›äº†APIæ¥å®žçŽ°æ‚²è§‚é”æœºåˆ¶åQˆé€šè¿‡SELECT FOR UPDATE è¯æ³•åQ?/strong>
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.
事务òq¶ä¸æ˜¯å¯æœ‰å¯æ— çš„åQŒæ‰€æœ‰çš„æ•°æ®åº“æ“作都是在事务环境下å‘生的åQ?strong>ä¸ç®¡æ˜¯read æ“作˜q˜æ˜¯writeæ“作。这点一定è¦å明¼‹®ï¼Œä»¥å‰æ›„¡»è¯¯è§£ã€?
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.
最好关é—自动æäº¤åŠŸèƒ½ï¼Œå› äØ“ä½¿ç”¨å¤§é‡çš„çŸæ•°æ®åº“事务在性能上未必比一个清晰定义的unit work强,åŽè€…æ›´åŠ æ˜“äºŽç»´æŠ¤å’Œæ‰©å±•ã€‚ï¼ˆä»¥å‰æ›„¡»è¯¯è§£åQ?
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的时候检查versionåQŒå¦‚æžœå‘现更改会抛出异常åQŒå¼€å‘äh员å¯ä»¥æ•æ‰è¯¥å¼‚常åQŒåƈ军_®šå¦‚何处ç†åQˆè¦ä¹ˆåˆòq¶ä¿®æ”¹ï¼Œè¦ä¹ˆæ”‘Ö¼ƒä¿®æ”¹åQ?
悲观é”åŸºæœ¬ä¸Šæ˜¯é æ•°æ®åº“本íw«æä¾›çš„æœºåˆ¶æ¥å®žçŽŽÍ¼Œé€šå¸¸åªè¦ä¸ºjdbc connection指定事务隔离的çñ”别å³å¯ã€‚The LockMode class defines the different lock levels that may be acquired by Hibernate.
悲观é”ä¼šåœ¨è¯»å–æ•°æ®ä¹‹å‰èŽ·å–ï¼Œå› æ¤èƒ½å¤Ÿä¿è¯å½“å‰äº‹åŠ¡çš„æ•°æ®å°±æ˜¯æœ€æ–°æ•°æ®ã€?
1ã€æ ‡é‡æŸ¥è¯¢ï¼ˆScalar queriesåQ‰ï¼š˜q”回的记录数¾l„的集åˆ
æ ‡é‡æ˜¯ä¸Žæ•°ç»„相对的概念,是一¿Uç”±å•一的值æ¥è¡¨å¾çš„é‡ã€‚æ‰€è°“æ ‡é‡æŸ¥è¯¢ï¼Œž®±æ˜¯ç›´æŽ¥ä½¿ç”¨åŽŸç”Ÿsqlè¯å¥çš„æŸ¥è¯¢ï¼Œä¹Ÿå³˜q”回的是裸数æ®ï¼ˆåªä¸˜q‡hibernate使用ResultSetMetaDataž®†resultsetä¸çš„æ¯ä¸€æ¡è®°å½•放åˆîC¸€ä¸ªæ•°¾l„Object[]ä¸?/code>åQ‰ã€?/p>
比如
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ã€å®žä½“查è¯?Entity queries)åQšè¿”回的是实体对象的集åˆ
3ã€Loadæ–ÒŽ³•åQšlookup by identifier åQŒè¿”回的是该identifier 对应的对è±?/strong>

凿 è§‚æ“v 2007-07-27 11:09 å‘表评论]]>
javaè¯è¨€ä¸çš„bit ¿UÖM½æ“作 http://www.aygfsteel.com/cherishchen/archive/2007/07/27/132717.html凿 è§‚æ“v 凿 è§‚æ“v Fri, 27 Jul 2007 03:08:00 GMT http://www.aygfsteel.com/cherishchen/archive/2007/07/27/132717.html http://www.aygfsteel.com/cherishchen/comments/132717.html http://www.aygfsteel.com/cherishchen/archive/2007/07/27/132717.html#Feedback 0 http://www.aygfsteel.com/cherishchen/comments/commentRss/132717.html http://www.aygfsteel.com/cherishchen/services/trackbacks/132717.html java 使“作符åQ?/strong>
å–ååQ?/strong>~x
- flips each bit to the opposite value.
与æ“作:AND
x & y
- AND operation between the corresponding bits in x and y.
或æ“作:OR
x | y
- OR operation between the corresponding bits in x and y.
异或æ“作åQšXOR
x ^ y
- XOR operation between the corresponding bits in x and y.
左移æ“作åQšShift left
x << y
- shifts x to the left by y bits. The high order bits are lost while zeros fill the right bits.
ž®†x左移yä½ï¼Œ¿UÕdЍ˜q‡ç¨‹ä¸ï¼Œé«˜ä½ä¼šä¸¢å¤?
有符åäh•°å³ç§»æ“作åQšShift Right - Signed
x >> y
- shifts x to the right by y bits. The low order bits are lost while the sign bit value (0 for positive numbers, 1 for negative) fills in the left bits.
æ— ç¬¦åäh•°å³ç§»åQšShift Right - Unsigned
x >>> y
- shifts x to the right by y bits. The low order bits are lost while zeros fill in the left bits regardless of the sign.
例ååQ?/strong>
下é¢çš„ä¾‹åæ˜¾½Cºå¦‚何将一个int数组通过¿UÖM½æ“作压羃åˆîC¸€ä¸ªint内ä¿å˜ï¼Œå…¶åŽŸç†æ˜¯åœ¨javaè¯è¨€ä¸ï¼Œint¾cÕdž‹ä½¿ç”¨4 bytesæ¥ä¿å˜ï¼Œå› æ¤å¯¹äºŽéœ€è¦åŽ‹¾~©çš„int数组åQŒå…¶ä¸çš„æ¯ä¸€ä¸ªint值的大å°ä¸èƒ½‘…过255åQ?çš?‹Æ¡æ–¹-1åQ‰ï¼Œå› 椘q™åªæ˜¯ä¸€ä¸ªå®žä¾‹ï¼š
int [] aRGB = {0x56, 0x78, 0x9A, 0xBC}; // 是用16˜q›åˆ¶ä¿å˜çš?¿U颜色å€?br> int color_val = aRGB[3];
color_val = color_val | (aRGB[2] << 8); // ä¸ÞZº†åŽ‹ç¾ƒåQŒéœ€è¦æ”¾¾|®åˆ°color_val值的½W¬äºŒä¸ªå—节佾|®ä¸ŠåQšå°†aRGB[2] 左移到第二个byteåQŒåŒæ—¶ä¸Žcolor_val˜q›è¡Œæˆ–æ“作,下é¢åŒç†
color_val = color_val | (aRGB[1] << 16);
color_val = color_val | (aRBG[0] << 24);
æ“作完的¾l“æžœæ˜?6 78 9A BC
如果è¦ä»ŽcolorVal ˜q˜åŽŸä¸ºint数组åQŒæˆ–者得到数¾l„ä¸çš„æŸä¸ªå€û|¼Œåªéœ€è¦å¯¹colorVal ˜q›è¡Œç›¸åº”的峿UÀL“作å³å¯ï¼š
int alpha_val = (colorVal >>> 24) & 0xFF;
int red_val = (colorVal >>> 16) & 0xFF;
int green_val = (colorVal >>> 8) & 0xFF;
int blue_val = colorVal & 0xFF;
å‚考资料:

凿 è§‚æ“v 2007-07-27 11:08 å‘表评论]]> 事务相关资料 http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132554.html凿 è§‚æ“v 凿 è§‚æ“v Thu, 26 Jul 2007 07:07:00 GMT http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132554.html http://www.aygfsteel.com/cherishchen/comments/132554.html http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132554.html#Feedback 0 http://www.aygfsteel.com/cherishchen/comments/commentRss/132554.html http://www.aygfsteel.com/cherishchen/services/trackbacks/132554.html ACID (åŽŸåæ€§ã€ä¸€è‡´æ€§ã€å¤ç«‹æ€§å’ŒæŒä¹…æ€?åQ?
事务¾U§åˆ«åQ?
1>.READ UNCOMMITTED 效率é«?但å¯èƒ½ä¼šæœ‰è„æ•°æ®(ä¿®æ”¹æ•°æ®æ—¶ä¸æŽ’ä»–è¯?;
2>.READ COMMITTED é¿å…äº†è„æ•°æ®(ä¿®æ”¹æ•°æ®æ—¶æŽ’他读);
3>. REPEATABLE READ å¯ÒŽ»¡‘Ïx¡ä»¶çš„æ•°æ®è¡Œé›†˜q›è¡Œé”定åQŒä¸å…è®¸å…¶ä»–äº‹åŠ¡ä¿®æ”¹å’Œåˆ é™?
4>.SERIALABLE READ 最高çñ”åˆ?¼›æ¢å¯?æ•°æ®è¡Œé›†ç”šè‡³æ•´ä¸ªæ•°æ®è¡¨ï¼‰æ’å…¥åQŒå› æ¤èƒ½å¤Ÿé˜²æ¢å‰òå½Þp¯»çš„å‘ç”?
常用的事务属性:
1>.Required å¿…é¡»˜q行在事务ä¸,没有现æˆçš„就新å¾ä¸€ä¸?
2>.RequiresNew æŒ‡æ˜Žæ¯æ¬¡˜q行都必™åÀL–°å»ÞZ¸€ä¸ªäº‹åŠ?
3>.Supports å¦‚ç›®å‰æ£å¤„于事务ä¸çš„è¯å°±åŠ å…¥åˆîCº‹åŠ¡ä¸åŽ?没有ž®Þq®—äº?
4>.Mandatory 必须有一个事务æ£åœ¨è¿è¡Œè®©å…¶åŠ å…?å¦åˆ™æŠ›å‡ºå¼‚常;
5>.NotSupported ä¸éœ€è¦è¿è¡ŒäºŽä»ÖM½•事务ä¸?
6>.Never ä¸ä½†ä¸ä¼š˜q行于ä“Q何事务ä¸,如果å‘现˜q行在事务ä¸ä¼šæŠ›å‡ºå¼‚常ã€?/p>
凿 è§‚æ“v 2007-07-26 15:07 å‘表评论]]> Jakarta Commons Lang 功能 http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132492.html凿 è§‚æ“v 凿 è§‚æ“v Thu, 26 Jul 2007 04:14:00 GMT http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132492.html http://www.aygfsteel.com/cherishchen/comments/132492.html http://www.aygfsteel.com/cherishchen/archive/2007/07/26/132492.html#Feedback 1 http://www.aygfsteel.com/cherishchen/comments/commentRss/132492.html http://www.aygfsteel.com/cherishchen/services/trackbacks/132492.html 今天看到Jakarta Commons LangåQŒå‘现æä¾›äº†å¾ˆå¤šæœ‰ç”¨çš„功èƒ?记录下æ¥åQŒç•™å¾…ä»¥åŽæŸ¥é˜?/p>
The Commons Lang library provides much needed additions to the standard JDK's java.lang package. Very generic, very reusable components for everyday use.
lang.*
String manipulation -StringUtils, StringEscapeUtils, RandomStringUtils, Tokenizer, WordUtils
StringEscapeUtils contains methods to escape and unescape Java, JavaScript, HTML, XML and SQL.
RandomStringUtils speaks for itself. It's provides ways in which to generate pieces of text, such as might be used for default passwords.
Character handling - CharSetUtils, CharSet, CharRange, CharUtils
CharUtils exists for this purpose, while CharSetUtils exists for set-manipulation of Strings.Be careful, although CharSetUtils takes an argument of type String, it is only as a set of characters. For example, CharSetUtils.delete("testtest", "tr")
will remove all t's and all r's from the String, not just the String "tr".
JVM interaction - SystemUtils, CharEncoding
SystemUtils is a simple little class which makes it easy to find out information about which platform you are on�/p>
The CharEncoding class is also used to interact with the Java environment and may be used to see which character encodings are supported in a particular environment.
Serialization - SerializationUtils, SerializationException
Assorted functions - ObjectUtils, ClassUtils, ArrayUtils, BooleanUtils
ClassUtils is largely a set of helper methods for reflection. Of special note are the comparators hidden away in ClassUtils, useful for sorting Class and Package objects by name�/p>
ArrayUtils. This is a big one with many methods and many overloads of these methods so it is probably worth an in depth look here. Before we begin, assume that every method mentioned is overloaded for all the primitives and for Object. Also, the short-hand 'xxx' implies a generic primitive type, but usually also includes Object.
Flotsam - BitField, Validate
On reaching the end of our package, we are left with a couple of classes that haven't fit any of the topics so far.
The BitField class provides a wrapper class around the classic bitmask integer, whilst the Validate class may be used for assertions
lang.builder.*
HashCodeBuilder will save your day. It, and its buddies (EqualsBuilder, CompareToBuilder, ToStringBuilder), take care of the nasty bits while you focus on the important bits, like which fields will go into making up the hashcode.
帮助æä¾›equalsã€compareã€toStringæ–ÒŽ³•
lang.enums.* (formerly lang.enum)
lang.exception.*
lang.math.*
These include classes to represent ranges of numbers, a Fraction class, various utilities for random numbers, and the flagship class, NumberUtils which contains a handful of classic number functions.
lang.mutable.*
mutable package provides mutable wrappers for primitive values (such as int, long, etc.) and Object. These wrappers are simiar to the wrappers provided by the Java API, but allow the wrapped value to be changed without needing to create a separate wrapper object.
lang.text.*
It provides, amongst other classes, a replacement for StringBuffer named StrBuilder
, a class for substituting variables within a String named StrSubstitutor
and a replacement for StringTokenizer named StrTokenizer
. While somewhat ungainly, the Str
prefix has been used to ensure we don't clash with any current or future standard Java classes.
lang.time.*
These include a StopWatch for simple performance measurements and an optimised FastDateFormat class.

凿 è§‚æ“v 2007-07-26 12:14 å‘表评论]]>
Ö÷Õ¾Ö©Öë³ØÄ£°å£º
½´ïÏØ|
ͨÐíÏØ|
¸Õ²ìÏØ|
¼ªÊ×ÊÐ|
ÎÚÀ¼²ì²¼ÊÐ|
ÖÓÉ½ÏØ|
³þÐÛÊÐ|
ɳÑóÏØ|
ľÀï|
²ýÀÖÏØ|
¿ª½ÏØ|
¼ÎÐËÊÐ|
º£ÄÏÊ¡|
ÄÉÓºÏØ|
ºÎ÷ÊÐ|
Íâ»ã|
Íû¶¼ÏØ|
׿×ÊÏØ|
°¢¿ËÌÕÏØ|
»ôÇñÏØ|
ÆÕÀ¼µêÊÐ|
ÎÚÉóÆì|
ÌÒÔ°ÏØ|
¼ÎÀèÏØ|
ƽÀÖÏØ|
¹âÉ½ÏØ|
ºá·åÏØ|
¬ÁúÏØ|
Æ¤É½ÏØ|
ÁÙÒØÏØ|
°¢ÀÉÆÃË|
±±º£ÊÐ|
ÈðÀöÊÐ|
×ÛÒÕ|
Á鱦ÊÐ|
Ï°Ë®ÏØ|
Ð˳ÇÊÐ|
ÏÌ·áÏØ|
º×·åÏØ|
ºéÑÅÏØ|
ÎäÍþÊÐ|