锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 鏈榪戝Hibernate鐨凾hreadLocal Session妯″紡鏈夌偣鍏磋叮銆備簬鏄牴鎹浌鏅撻挗緲昏瘧鐨凥ibernate Reference鍋氫簡(jiǎn)涓皬嫻嬮獙錛岀粨鏋滃彂鐜頒簡(jiǎn)涓涓皬bug銆? public class HibernateUtil { public static final ThreadLocal session = new ThreadLocal(); 灝辮繖涔堢畝鍗曚竴涓▼搴忥紝榪愯鍒版渶鍚庯紝鍑虹幇涓涓敊璇?/p>
org.hibernate.SessionException: Session was already closed 閿欒鍑虹幇鍦?HibernateUtil.closeSession(); 榪欎竴琛岋紝鎰忔濇槸session宸茬粡鍏抽棴浜?jiǎn)锛屽啀娆″叧闂畠灏卞紩钃v寮傚父浜?jiǎn)銆?/p>
涓嶈繃鍓嶉潰鐨勪唬鐮佷腑鍙湁涓猼x.commit(); 鎻愪氦浜嬪姟 鑰屽凡錛屽茍娌℃湁鑷姩鍏抽棴session鍟婏紵 浜庢槸鎶奃EBUG淇℃伅璋冪敤鍑烘潵錛屽彂鐜頒簡(jiǎn)浠ヤ笅鍑犲彞鎻愮ず錛?
閭d箞榪欎釜鏈哄埗鏄庝箞鍙戠敓鐨勫憿錛?/p>
鎵撳紑浜?jiǎn)Hibernate3鐨勬簮鐮侊紝鎴戞壘鍒頒簡(jiǎn)絳旀銆? public org.hibernate.classic.Session getCurrentSession() throws HibernateException { 浠栬皟鐢ㄧ殑鏄竴涓猚urrentSessionContext鐨刢urrentSession鏂規(guī)硶銆傛煡鎵綾urrentSessionContext鍙橀噺錛?/p>
currentSessionContext = buildCurrentSessionContext(); 錛岀煡閬撲簡(jiǎn)buildCurrentSessionContext鏂規(guī)硶浜х敓浜?jiǎn)杩欎釜currentSessionContext 瀵硅薄銆?/p>
private CurrentSessionContext buildCurrentSessionContext() { if ( impl == null ) { 榪欎釜鏂規(guī)硶灝辨槸鐢ㄦ潵鍒ゆ柇浣跨敤JTA綆$悊榪欎釜SessionContext榪樻槸鐢═hreadLocal鏉ョ鐞哠essionContext鐨勩? 鎵懼埌璇ョ被鐨刢urrentSession鏂規(guī)硶 public final Session currentSession() throws HibernateException { 鐒跺悗璺熻釜鍒?buildOrObtainSession(),灝辨槸榪欓噷錛屾墦寮浜?jiǎn)session銆?/p>
protected Session buildOrObtainSession() { 錛屽氨鏄session鏄惁搴旇鍦ㄤ簨鍔℃彁浜ゅ悗鑷姩鍏抽棴銆?/p>
鐒跺悗鎵撳紑 ThreadLocalSessionContext 鐨刬sAutoCloseEnabled()鏂規(guī)硶銆?/p>
/** 絳旀灝卞湪榪欓噷錛屽氨鏄鍦═hreadLocal Session妯″紡涓嬮潰錛屽彧瑕佹彁浜や簡(jiǎn)浜嬪姟錛岄偅涔坰ession灝辮嚜鍔ㄥ叧闂簡(jiǎn)錛屽洜姝ゆ垜鍙傜収Hibernate Refernece涓婇潰鐨勪唬鐮佸啓鐨勫湪浜嬪姟鍏抽棴浠ュ悗鍐嶈皟鐢℉ibernateUtil.closeSession();鏄笉瀵圭殑錛岃繖鍙ヤ唬鐮佹槸瀹屽叏澶氫綑鐨勩?/p>
緙╃暐鏄劇ず聽(tīng)聽(tīng)聽(tīng)聽(tīng)
浠g爜寰堢畝鍗曪紝閮芥槸鍒╃敤Hibernate Reference涓幇鎴愮殑浠g爜銆?
棣栧厛鏄竴涓緟鍔╃殑寰楀埌綰跨▼瀹夊叏鐨剆ession鐨凥ibernateUtil綾伙紝
public static final SessionFactory sessionFactory;
static{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Throwable ex){
throw new ExceptionInInitializerError(ex);
}
}
public static Session currentSession()
{
Session s = (Session) session.get();
if (s==null )
{
s = sessionFactory.getCurrentSession();
session.set(s);
}
return s;
}
public static void closeSession()
{
Session s = (Session) session.get();
if (s!=null)
s.close();
session.set(null);
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
鐒跺悗鏄竴涓祴璇曟彃鍏ユ暟鎹殑浠g爜銆備篃寰堢畝鍗曪紝涔熸槸浠縃ibernate Reference涓婇潰鐨勪唬鐮併?
public class InsertUser {
public static void main(String[] args) {
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
TUser user = new TUser();
user.setName("Emma");
session.save(user);
tx.commit();
HibernateUtil.closeSession();
}
}
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.close(Unknown Source)
at Util.HibernateUtil.closeSession(HibernateUtil.java:36)
at test.InsertUser.main(InsertUser.java:20)
Exception in thread "main"
DEBUG [main] - after transaction completion
DEBUG [main] - automatically closing session
DEBUG [main] - closing session
DEBUG [main] - connection already null in cleanup : no action
DEBUG [main] - allowing proxied method [close] to proceed to real session
DEBUG [main] - closing session
org.hibernate.SessionException: Session was already closed
鐗瑰埆鏄笅闈㈣繖3鍙ヨ瘽寮曡搗浜?jiǎn)鎴戠殑娉ㄦ剰锛屾灉鐒舵槸session鍏抽棴浜?jiǎn)锛岃屼笖鏄湪 浜嬪姟緇撴潫浠ュ悗鑷姩鍏抽棴鐨勩?
DEBUG [main] - after transaction completion
DEBUG [main] - automatically closing session
DEBUG [main] - closing session
棣栧厛錛屾牴鎹畇essionFactory = new Configuration().configure().buildSessionFactory();
鎵撳紑Configuration綾葷殑buildSessionFactory()鏂規(guī)硶錛屾壘鍒皊essionFactory鐨勭敓鎴愯鍙?
return new SessionFactoryImpl(
this,
mapping,
settings,
getInitializedEventListeners()
);
錛岀劧鍚庢壘鍒癝essionFactoryImpl鐨刧etCurrentSession鏂規(guī)硶錛屽彂鐜版槸榪欎箞瀹氫箟鐨勩?/p>
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
}
String impl = properties.getProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS );
// for backward-compatability
if ( impl == null && transactionManager != null ) {
impl = "jta";
}
return null;
}
else if ( "jta".equals( impl ) ) {
return new JTASessionContext( this );
}
else if ( "thread".equals( impl ) ) {
return new ThreadLocalSessionContext( this );
}
else {
try {
Class implClass = ReflectHelper.classForName( impl );
return ( CurrentSessionContext ) implClass
.getConstructor( new Class[] { SessionFactoryImplementor.class } )
.newInstance( new Object[] { this } );
}
catch( Throwable t ) {
log.error( "Unable to construct current session context [" + impl + "]", t );
return null;
}
}
}
鍦ㄦ垜浠繖閲屾槸鐢?ThreadLocal 鏉ョ鐞嗙殑錛屼簬鏄壘鍒頒簡(jiǎn)currentSessionContext 鐨勫疄鐜扮被鏄?ThreadLocalSessionContext銆?/p>
Session current = existingSession( factory );
if (current == null) {
current = buildOrObtainSession();
// register a cleanup synch
current.getTransaction().registerSynchronization( buildCleanupSynch() );
// wrap the session in the transaction-protection proxy
if ( needsWrapping( current ) ) {
current = wrap( current );
}
// then bind it
doBind( current, factory );
}
return current;
}
return factory.openSession(
null,
isAutoFlushEnabled(),
isAutoCloseEnabled(),
getConnectionReleaseMode()
);
}
娉ㄦ剰絎笁涓弬鏁幫細(xì)isAutoCloseEnabled
鎵撳紑Session榪欎釜鎺ュ彛錛岀湅鍒?openSession鏂規(guī)硶涓繖涓弬鏁版槸濡備笅鎻忚堪鐨勶細(xì)
* @param autoCloseSessionEnabled Should the session be auto-closed after
* transaction completion?
* Mainly for subclass usage. This impl always returns true.
*
* @return Whether or not the the session should be closed by transaction completion.
*/
protected boolean isAutoCloseEnabled() {
return true;
}
鐪嬪埌濡備笅鎻愮ず錛歐hether or not the the session should be closed by transaction completion 錛屽嵆鏃犺濡備綍session搴旇鍦ㄤ簨鍔″畬鎴愬悗鍏抽棴銆?/p>
]]>
select * from testdata!customer;
聽(tīng)聽(tīng)聽(tīng)聽(tīng)left outer join testdata!orders;
聽(tīng)聽(tīng)聽(tīng)聽(tīng)on customer.cust_id=orders.cust_id
聽(tīng)聽(tīng)聽(tīng)聽(tīng)
濡傛灉鏄乏榪炴帴璇ュ懡浠ゅ皢榪斿洖
join
宸︿晶琛?/span>
(customer)
涓殑鎵鏈夎褰曚互鍙?qiáng)鍙充晶琛ㄤ腑鍖归厤鐨勮褰曞Q涘彸榪炴帴鍒欏弽涔嬶紝榪斿洖
orders
涓墍鏈夎褰曞拰
customer
鐨勫尮閰嶈褰曪紱瀹屽叏榪炴帴榪斿洖涓や釜琛ㄤ腑鐨勬墍鏈夎褰曘?/span>
聽(tīng)
鍙寵繛鎺ヨ鏄庣瓑鍙峰彸渚х殑鎵鏈夎褰曞潎浼?xì)琚槃·哄Q屾棤璁哄叾鍦ㄥ乏渚ф槸鍚﹀緱鍒板尮閰嶏紝
甯歌閿欒錛?span lang="EN-US">
1
錛?span lang="EN-US">Caused by: org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
濡傛灉鍑虹幇榪欒閿欒璇存槑浣犵殑XML閰嶇疆鏂囦歡鏈変笉瑙勮寖鐨勫瓧絎︼紝媯(gè)鏌ヤ笅銆?
2
錛?span lang="EN-US">net.sf.hibernate.MappingException: Error reading resource: hibernate/Hello_Bean.hbm.xml
濡傛灉鍑虹幇榪欒閿欒璇存槑浣犵殑hibernate鐨?span lang="EN-US">XML閰嶇疆鏂囦歡鏈夐敊
3
錛?span lang="EN-US">net.sf.hibernate.MappingException: Resource: hibernate/Hello_Bean.hbm.xml not found
濡傛灉鍑虹幇榪欒閿欒璇存槑hibernate鐨?span lang="EN-US">XML閰嶇疆鏂囦歡娌℃湁鎵懼埌錛屼綘搴旇鎶?span lang="EN-US">XML鏂囦歡鏀懼湪涓庝綘鐨勭被鏂囦歡鍚屼釜鐩綍涓?span lang="EN-US">,鏈枃涓槸鏀懼湪hibernate\classes\hibernate\鐩綍涓嬶紝涔熷氨鏄窡Hello_Bean.class綾繪枃浠朵竴璧楓?
4
錛?span lang="EN-US">net.sf.hibernate.PropertyNotFoundException: Could not find a setter for property name in class hibernate.Hello_Bean
濡傛灉鍑虹幇榪欒閿欒璇存槑浣犵殑xml鏂囦歡閲岃緗殑瀛楁鍚?span lang="EN-US">name鐨勫間笌Hello_Bean.Java綾婚噷鐨?span lang="EN-US">getXXX鎴?span lang="EN-US">setXXX鏂規(guī)硶涓嶄竴鑷淬?span lang="EN-US">
5
錛?span lang="EN-US">net.sf.hibernate.HibernateException: JDBC Driver class not found: org.gjt.mm.mysql.Driver
6.The database returned no natively generated identity value
銆?/span>
聽(tīng)聽(tīng)聽(tīng)聽(tīng)
涓婚敭娌℃湁娣誨姞澧為噺
絎竴閮ㄥ垎:聽(tīng) jsp聽(tīng) ---> java/servlet
瀵逛簬嫻佽鐨?span lang="EN-US">Tomcat鏉ヨ錛屾湁浠ヤ笅涓夌瑙e喅鏂規(guī)硶錛?/p>
銆銆1) 鏇存敼 D:\Tomcat\conf\server.xml錛屾寚瀹氭祻瑙堝櫒鐨勭紪鐮佹牸寮忎負(fù)鈥?/span>綆浣撲腑鏂?span lang="EN-US">鈥?/span>錛?/p>
銆銆鏂規(guī)硶鏄壘鍒?span lang="EN-US"> server.xml
聽(tīng)聽(tīng)聽(tīng) <Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
聽(tīng)聽(tīng)聽(tīng) enableLookups="false" redirectPort="8443" acceptCount="100"
聽(tīng)聽(tīng)聽(tīng) connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
銆銆鏍囪錛岀矖浣撳瓧鏄垜娣誨姞鐨勩?/p>
銆銆鍙互榪欐牱楠岃瘉浣犵殑鏇存敼鏄惁鎴愬姛錛氬湪鏇存敼鍓嶏紝鍦ㄤ綘鍑虹幇涔辯爜鐨勯〉闈㈢殑IE嫻忚鍣紝鐐瑰嚮鑿滃崟鈥?/span>鏌ョ湅锝滅紪鐮?span lang="EN-US">鈥?/span>錛屼細(xì)鍙戠幇鈥?/span>瑗挎(ISO)鈥?/span>澶勪簬閫変腑鐘舵併傝屾洿鏀瑰悗錛岀偣鍑昏彍鍗?span lang="EN-US">鈥?/span>鏌ョ湅锝滅紪鐮?span lang="EN-US">鈥?/span>錛屼細(xì)鍙戠幇鈥?/span>綆浣撲腑鏂?span lang="EN-US">(GB2312)鈥?/span>澶勪簬閫変腑鐘舵併傛敞鎰忎綘鐨凾omcat鐨刢ommon涓嬭鏈?endorsed"鍜?i18n"鏂囦歡澶癸紝鍏朵腑鍖呭惈緙栫爜鎵闇鐨刲ib
聽(tīng)聽(tīng)聽(tīng)
銆銆2)鏇磋 Java 紼嬪簭錛?br />聽(tīng)public class ThreeParams extends HttpServlet {
聽(tīng) public void doGet(HttpServletRequest request, HttpServletResponse response)
聽(tīng)聽(tīng) throws ServletException, IOException {
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng) response.setContentType("text/html; charset=GBK");
聽(tīng)聽(tīng)聽(tīng)聽(tīng)聽(tīng) ...
聽(tīng) }聽(tīng)
}
銆銆綺椾綋瀛楁槸蹇呴渶瑕佹湁鐨勶紝瀹冪殑浣滅敤鏄嫻忚鍣ㄦ妸Unicode瀛楃杞崲涓?span lang="EN-US">GBK瀛楃銆傝繖鏍烽〉闈㈢殑鍐呭鍜屾祻瑙堝櫒鐨勬樉紺烘ā寮忛兘璁炬垚浜?span lang="EN-US">GBK錛屽氨涓嶄細(xì)涔辯爜浜?jiǎn)銆?/p>
聽(tīng)聽(tīng)
絎簩閮ㄥ垎聽(tīng)聽(tīng)聽(tīng) java/servlet聽(tīng)聽(tīng) ---->聽(tīng)聽(tīng) mysql
mysql鍜孒ibernate涓殑涓枃闂瑙e喅鏂規(guī)硶
鍙︿竴縐嶈В鍐蟲柟娉?mysql-4.1.11).浣庣増鏈殑mysql浼間箮娌″嚭鐜拌繃涓枃闂(mysql-4.0.17). :)
1.娣誨姞涓涓繃婊ゅ櫒,灝嗗瓧絎﹂泦緙栫爜璁句負(fù)GBK.
淇敼web.xml:
聽(tīng)<filter>
聽(tīng)聽(tīng)<filter-name>SetCharacterEncoding</filter-name>
聽(tīng)聽(tīng)<filter-class>hxz.filter.SetEncodingFilter</filter-class>
聽(tīng)聽(tīng)<init-param>
聽(tīng)聽(tīng)聽(tīng)<param-name>encoding</param-name>
聽(tīng)聽(tīng)聽(tīng)<param-value>GBK</param-value>
聽(tīng)聽(tīng)</init-param>
聽(tīng)</filter>
聽(tīng)
聽(tīng)<filter-mapping>
聽(tīng)聽(tīng)<filter-name>SetCharacterEncoding</filter-name>
聽(tīng)聽(tīng)<url-pattern>/*</url-pattern>
聽(tīng)</filter-mapping>
鏂板緩涓涓繃婊ゅ櫒:
package hxz.filter;
import java.io.IOException;
import javax.servlet.*;
public class SetEncodingFilter implements Filter {
聽(tīng)
聽(tīng)// default character encoding
聽(tīng)String defaultEncoding = "GBK";聽(tīng)
聽(tīng)
聽(tīng)public void init(FilterConfig config) throws ServletException {
聽(tīng)聽(tīng)String encoding = config.getInitParameter("encoding");
聽(tīng)聽(tīng)if (encoding != null) {
聽(tīng)聽(tīng)聽(tīng)defaultEncoding = encoding;
聽(tīng)聽(tīng)}
聽(tīng)}
聽(tīng)public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
聽(tīng)聽(tīng)request.setCharacterEncoding(defaultEncoding);
聽(tīng)聽(tīng)chain.doFilter(request, response);
聽(tīng)}
聽(tīng)public void destroy() {
聽(tīng)聽(tīng)defaultEncoding = null;
聽(tīng)}
}
2.淇敼hibernate閰嶇疆鏂囦歡:
<property name="url">
聽(tīng) <value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK</value>
</property>
娉ㄦ剰鍚庨潰鐨?useUnicode=true&characterEncoding=GBK, 鍦▁ml涓?amp;瑕佹敼涓?amp;
3.鍦ㄦ柊鐗堟湰涓璵ysql寤鴻〃鏃?浣犲彲浠ラ夋嫨涓縐嶅瓧絎﹂泦,灝嗗畠璁句負(fù)GBK.
jsp鎴杝ervlet涓妸瀛楃闆嗘敼涓篏BK.
聽(tīng)