锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产精品无码专区影院,中文字幕亚洲精品无码,亚洲日韩一区精品射精http://www.tkk7.com/kevinfriend/category/12776.htmlzh-cnFri, 02 Mar 2007 05:49:58 GMTFri, 02 Mar 2007 05:49:58 GMT60jdbc-batch processinghttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74550.html搴鋒枃搴鋒枃Wed, 11 Oct 2006 06:40:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74550.htmlhttp://www.tkk7.com/kevinfriend/comments/74550.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74550.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/74550.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/74550.htmlthe addBatch() method is basically nothing more than a tool fro assigning a bunch of sql statements to a jdbc statement object for execution together

PreparedStatement stmt=conn.prepareStatement(
聽聽聽聽聽 "update account set balance=? where acct_id=?");
int[] rows;
for(int i=0;i<accts.length;i++){
聽聽聽 stmt.setInt(1,i);
聽聽聽 stmt.setLong(2,i);
聽聽聽 stmt.addBatch();
聽 }
rows=stemt.executeBatch();



搴鋒枃 2006-10-11 14:40 鍙戣〃璇勮
]]>
緲婚〉鐨勫疄鐜?/title><link>http://www.tkk7.com/kevinfriend/archive/2006/10/11/74549.html</link><dc:creator>搴鋒枃</dc:creator><author>搴鋒枃</author><pubDate>Wed, 11 Oct 2006 06:39:00 GMT</pubDate><guid>http://www.tkk7.com/kevinfriend/archive/2006/10/11/74549.html</guid><wfw:comment>http://www.tkk7.com/kevinfriend/comments/74549.html</wfw:comment><comments>http://www.tkk7.com/kevinfriend/archive/2006/10/11/74549.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/kevinfriend/comments/commentRss/74549.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/kevinfriend/services/trackbacks/74549.html</trackback:ping><description><![CDATA[ <p>1 oracle 鐨勫疄鐜?br />聽璇彞涓<br />SELECT ID, [FIELD_NAME,...] FROM TABLE_NAME WHERE ID IN ( SELECT ID FROM (SELECT ROWNUM AS NUMROW, ID FROM TABLE_NAME WHERE 鏉′歡1 ORDER BY 鏉′歡2) WHERE NUMROW > 80 AND NUMROW < 100 ) ORDER BY 鏉′歡3; <br />璇彞浜岋細<br />SELECT * FROM (( SELECT ROWNUM AS NUMROW, c.* from (select [FIELD_NAME,...] FROM TABLE_NAME WHERE 鏉′歡1 ORDER BY 鏉′歡2) c) WHERE NUMROW > 80 AND NUMROW < 100 ) ORDER BY 鏉′歡3;</p> <p>select * from (select rownum as numrow from table_name where numrow>80 and numrow<100 )<br />涓嶈兘鐩存帴浣跨敤 select * from rownum>100 and rownum<200; <br />in oracle return null;<br />2 sql server 鐨勫疄鐜?br />3 mysql 鐨勫疄鐜?/p> <p>select id from table_name where id in<br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 select * from (select rownum as numrow ,id from tabl_name) <br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 where numrow>80 and num<100;聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <br />聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 </p> <img src ="http://www.tkk7.com/kevinfriend/aggbug/74549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/kevinfriend/" target="_blank">搴鋒枃</a> 2006-10-11 14:39 <a href="http://www.tkk7.com/kevinfriend/archive/2006/10/11/74549.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>jdbc-prepare sqlhttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74548.html搴鋒枃搴鋒枃Wed, 11 Oct 2006 06:38:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74548.htmlhttp://www.tkk7.com/kevinfriend/comments/74548.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74548.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/74548.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/74548.htmloracle provide two kinds of prepared SQL prepared statements and store procedures.Prepared SQL provide an advantage over the simple sql statements you have convered so far.if you execute the same prepared sql more than once,the database remains ready for your sql without having to rebuild the query plan.
聽1) Prepared Statements
聽PreparedStatement statement=conn.preparedStatement(
聽聽聽聽 "update account set balance=? where id=?");
聽for(int i=0;i<accounts.length;i++){
聽聽聽 statement.setFloat(1,accounts[i].getBalance());
聽聽聽 statement.setInt(2,i);
聽聽聽 statement.execut();
聽聽聽 stateement.clearParameters();
聽}
聽commit();
聽statement.close;
聽2) Stored Procedure
聽try {
聽聽聽 CallableStatement statement;
聽聽聽 int i;

聽聽聽 statement = c.prepareCall("{call sp_interest[(?,?)]}");

聽聽聽 statement.registerOutParameter(2, java.sql.Types.FLOAT);
聽聽聽 for(i=1; i<accounts.length; i++) {
聽聽聽聽聽聽聽 statement.setInt(1, accounts[i].getId( ));
聽聽聽聽聽聽聽 statement.execute( );
聽聽聽聽聽聽聽 System.out.println("New balance: " + statement.getFloat(2));
聽聽聽 }
聽聽聽 c.commit( );
聽聽聽 statement.close( );
聽聽聽 c.close( );
}

搴鋒枃 2006-10-11 14:38 鍙戣〃璇勮
]]>
java 璋冪敤瀛樺偍榪囩▼ 杞澆http://www.tkk7.com/kevinfriend/archive/2006/10/11/74547.html搴鋒枃搴鋒枃Wed, 11 Oct 2006 06:37:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74547.htmlhttp://www.tkk7.com/kevinfriend/comments/74547.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/10/11/74547.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/74547.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/74547.html鏈枃闃愯堪浜嗘庝箞浣跨敤DBMS瀛樺偍榪囩▼銆傛垜闃愯堪浜嗕嬌鐢ㄥ瓨鍌ㄨ繃紼嬬殑鍩烘湰鐨勫拰楂樼駭鐗規э紝姣斿榪斿洖ResultSet銆傛湰鏂囧亣璁句綘瀵笵BMS鍜孞DBC宸茬粡闈炲父鐔熸倝錛屼篃鍋囪浣犺兘澶熸鏃犻殰紕嶅湴闃呰鍏跺畠璇█鍐欐垚鐨勪唬鐮侊紙鍗充笉鏄疛ava鐨勮璦錛夛紝浣嗘槸錛屽茍涓嶈姹備綘鏈変換浣曞瓨鍌ㄨ繃紼嬬殑緙栫▼緇忓巻銆?
瀛樺偍榪囩▼鏄寚淇濆瓨鍦ㄦ暟鎹簱騫跺湪鏁版嵁搴撶鎵ц鐨勭▼搴忋備綘鍙互浣跨敤鐗規畩鐨勮娉曞湪Java綾諱腑璋冪敤瀛樺偍榪囩▼銆傚湪璋冪敤鏃訛紝瀛樺偍榪囩▼鐨勫悕縐板強鎸囧畾鐨勫弬鏁伴氳繃JDBC榪炴帴鍙戦佺粰DBMS錛屾墽琛屽瓨鍌ㄨ繃紼嬪茍閫氳繃榪炴帴錛堝鏋滄湁錛夎繑鍥炵粨鏋溿?
浣跨敤瀛樺偍榪囩▼鎷ユ湁鍜屼嬌鐢ㄥ熀浜嶦JB鎴朇ORBA榪欐牱鐨勫簲鐢ㄦ湇鍔″櫒涓鏍風殑濂藉銆傚尯鍒槸瀛樺偍榪囩▼鍙互浠庡緢澶氭祦琛岀殑DBMS涓厤璐逛嬌鐢紝鑰屽簲鐢ㄦ湇鍔″櫒澶ч兘闈炲父鏄傝吹銆傝繖騫朵笉鍙槸璁稿彲璇佽垂鐢ㄧ殑闂銆備嬌鐢ㄥ簲鐢ㄦ湇鍔″櫒鎵闇瑕佽姳璐圭殑綆$悊銆佺紪鍐欎唬鐮佺殑璐圭敤錛屼互鍙婂鎴風▼搴忔墍澧炲姞鐨勫鏉傛э紝閮藉彲浠ラ氳繃DBMS涓殑瀛樺偍榪囩▼鎵鏁翠釜鍦版浛浠c?
浣犲彲浠ヤ嬌鐢↗ava錛孭ython錛孭erl鎴朇緙栧啓瀛樺偍榪囩▼錛屼絾鏄氬父浣跨敤浣犵殑DBMS鎵鎸囧畾鐨勭壒瀹氳璦銆侽racle浣跨敤PL/SQL錛孭ostgreSQL浣跨敤pl/pgsql錛孌B2浣跨敤Procedural SQL銆傝繖浜涜璦閮介潪甯哥浉浼箋傚湪瀹冧滑涔嬮棿縐繪瀛樺偍榪囩▼騫朵笉姣斿湪Sun鐨凟JB瑙勮寖涓嶅悓瀹炵幇鐗堟湰涔嬮棿縐繪Session Bean鍥伴毦銆傚茍涓旓紝瀛樺偍榪囩▼鏄負宓屽叆SQL鎵璁捐錛岃繖浣垮緱瀹冧滑姣擩ava鎴朇絳夎璦鏇村姞鍙嬪ソ鍦版柟寮忚〃杈炬暟鎹簱鐨勬満鍒躲?
鍥犱負瀛樺偍榪囩▼榪愯鍦―BMS鑷韓錛岃繖鍙互甯姪鍑忓皯搴旂敤紼嬪簭涓殑絳夊緟鏃墮棿銆備笉鏄湪Java浠g爜涓墽琛?涓垨5涓猄QL璇彞錛岃屽彧闇瑕佸湪鏈嶅姟鍣ㄧ鎵ц1涓瓨鍌ㄨ繃紼嬨傜綉緇滀笂鐨勬暟鎹線榪旀鏁扮殑鍑忓皯鍙互鎴忓墽鎬у湴浼樺寲鎬ц兘銆?

浣跨敤瀛樺偍榪囩▼

綆鍗曠殑鑰佺殑JDBC閫氳繃CallableStatement綾繪敮鎸佸瓨鍌ㄨ繃紼嬬殑璋冪敤銆傝綾誨疄闄呬笂鏄疨reparedStatement鐨勪竴涓瓙綾匯傚亣璁炬垜浠湁涓涓猵oets鏁版嵁搴撱傛暟鎹簱涓湁涓涓緗瘲浜洪濅笘騫撮緞鐨勫瓨鍌ㄨ繃紼嬨備笅闈㈡槸瀵硅侀厭楝糄ylan Thomas錛坥ld soak Dylan Thomas錛屼笉鎸囧畾鏄惁鏈夊叧鍏告晠銆佹枃鍖栵紝璇鋒壒璇勬寚姝c傝瘧娉級榪涜璋冪敤鐨勮緇嗕唬鐮侊細

try{
int age = 39;

String poetName = "dylan thomas";

CallableStatement proc = connection.prepareCall("{ call set_death_age(?, ?) }");

proc.setString(1, poetName);

proc.setInt(2, age);

cs.execute();

}catch (SQLException e){ // ....}

浼犵粰prepareCall鏂規硶鐨勫瓧涓叉槸瀛樺偍榪囩▼璋冪敤鐨勪功鍐欒鑼冦傚畠鎸囧畾浜嗗瓨鍌ㄨ繃紼嬬殑鍚嶇О錛岋紵浠h〃浜嗕綘闇瑕佹寚瀹氱殑鍙傛暟銆?
鍜孞DBC闆嗘垚鏄瓨鍌ㄨ繃紼嬬殑涓涓緢澶х殑渚垮埄錛氫負浜嗕粠搴旂敤涓皟鐢ㄥ瓨鍌ㄨ繃紼嬶紝涓嶉渶瑕佸瓨鏍癸紙stub錛夌被鎴栬呴厤緗枃浠訛紝闄や簡浣犵殑DBMS鐨凧DBC椹卞姩紼嬪簭澶栦粈涔堜篃涓嶉渶瑕併?
褰撹繖孌典唬鐮佹墽琛屾椂錛屾暟鎹簱鐨勫瓨鍌ㄨ繃紼嬪氨琚皟鐢ㄣ傛垜浠病鏈夊幓鑾峰彇緇撴灉錛屽洜涓鴻瀛樺偍榪囩▼騫朵笉榪斿洖緇撴灉銆傛墽琛屾垚鍔熸垨澶辮觸灝嗛氳繃渚嬪寰楃煡銆傚け璐ュ彲鑳芥剰鍛崇潃璋冪敤瀛樺偍榪囩▼鏃剁殑澶辮觸錛堟瘮濡傛彁渚涚殑涓涓弬鏁扮殑綾誨瀷涓嶆紜級錛屾垨鑰呬竴涓簲鐢ㄧ▼搴忕殑澶辮觸錛堟瘮濡傛姏鍑轟竴涓緥澶栨寚紺哄湪poets鏁版嵁搴撲腑騫朵笉瀛樺湪鈥淒ylan Thomas鈥濓級

緇撳悎SQL鎿嶄綔涓庡瓨鍌ㄨ繃紼?

鏄犲皠Java瀵硅薄鍒癝QL琛ㄤ腑鐨勮鐩稿綋綆鍗曪紝浣嗘槸閫氬父闇瑕佹墽琛屽嚑涓猄QL璇彞錛涘彲鑳芥槸涓涓猄ELECT鏌ユ壘ID錛岀劧鍚庝竴涓狪NSERT鎻掑叆鎸囧畾ID鐨勬暟鎹傚湪楂樺害瑙勬牸鍖栵紙絎﹀悎鏇撮珮鐨勮寖寮忥紝璇戞敞錛夌殑鏁版嵁搴撴ā寮忎腑錛屽彲鑳介渶瑕佸涓〃鐨勬洿鏂幫紝鍥犳闇瑕佹洿澶氱殑璇彞銆侸ava浠g爜浼氬緢蹇湴鑶ㄨ儉錛屾瘡涓涓鍙ョ殑緗戠粶寮閿涔熻繀閫熷鍔犮?
灝嗚繖浜汼QL璇彞杞Щ鍒頒竴涓瓨鍌ㄨ繃紼嬩腑灝嗗ぇ澶х畝鍖栦唬鐮侊紝浠呮秹鍙婁竴嬈$綉緇滆皟鐢ㄣ傛墍鏈夊叧鑱旂殑SQL鎿嶄綔閮藉彲浠ュ湪鏁版嵁搴撳唴閮ㄥ彂鐢熴傚茍涓旓紝瀛樺偍榪囩▼璇█錛屼緥濡侾L/SQL錛屽厑璁鎬嬌鐢⊿QL璇硶錛岃繖姣擩ava浠g爜鏇村姞鑷劧銆備笅闈㈡槸鎴戜滑鏃╂湡鐨勫瓨鍌ㄨ繃紼嬶紝浣跨敤Oracle鐨凱L/SQL璇█緙栧啓錛?

create procedure set_death_age(poet VARCHAR2, poet_age NUMBER)

poet_id NUMBER;

begin SELECT id INTO poet_id FROM poets WHERE name = poet;

INSERT INTO deaths (mort_id, age) VALUES (poet_id, poet_age);

end set_death_age;

寰堢嫭鐗癸紵涓嶃傛垜鎵撹祵浣犱竴瀹氭湡寰呯湅鍒頒竴涓猵oets琛ㄤ笂鐨刄PDATE銆傝繖涔熸殫紺轟簡浣跨敤瀛樺偍榪囩▼瀹炵幇鏄涔堝鏄撶殑涓浠朵簨鎯呫俿et_death_age鍑犱箮鍙互鑲畾鏄竴涓緢鐑傜殑瀹炵幇銆傛垜浠簲璇ュ湪poets琛ㄤ腑娣誨姞涓鍒楁潵瀛樺偍閫濅笘騫撮緞銆侸ava浠g爜涓茍涓嶅叧蹇冩暟鎹簱妯″紡鏄庝箞瀹炵幇鐨勶紝鍥犱負瀹冧粎璋冪敤瀛樺偍榪囩▼銆傛垜浠互鍚庡彲浠ユ敼鍙樻暟鎹簱妯″紡浠ユ彁楂樻ц兘錛屼絾鏄垜浠笉蹇呬慨鏀規垜浠唬鐮併?
涓嬮潰鏄皟鐢ㄤ笂闈㈠瓨鍌ㄨ繃紼嬬殑Java浠g爜錛?

public static void setDeathAge(Poet dyingBard, int age) throws SQLException{

Connection con = null;

CallableStatement proc = null;

try {

con = connectionPool.getConnection();

proc = con.prepareCall("{ call set_death_age(?, ?) }");

proc.setString(1, dyingBard.getName());

proc.setInt(2, age);

proc.execute();

}聽

finally {

try { proc.close(); }

catch (SQLException e) {}

con.close();

}

}

涓轟簡紜繚鍙淮鎶ゆэ紝寤鴻浣跨敤鍍忚繖鍎胯繖鏍風殑static鏂規硶銆傝繖涔熶嬌寰楄皟鐢ㄥ瓨鍌ㄨ繃紼嬬殑浠g爜闆嗕腑鍦ㄤ竴涓畝鍗曠殑妯$増浠g爜涓傚鏋滀綘鐢ㄥ埌璁稿瀛樺偍榪囩▼錛屽氨浼氬彂鐜頒粎闇瑕佹嫹璐濄佺矘璐村氨鍙互鍒涘緩鏂扮殑鏂規硶銆傚洜涓轟唬鐮佺殑妯$増鍖栵紝鐢氳嚦涔熷彲浠ラ氳繃鑴氭湰鑷姩鐢熶駭璋冪敤瀛樺偍榪囩▼鐨勪唬鐮併?

Functions

瀛樺偍榪囩▼鍙互鏈夎繑鍥炲鹼紝鎵浠allableStatement綾繪湁綾諱技getResultSet榪欐牱鐨勬柟娉曟潵鑾峰彇榪斿洖鍊箋傚綋瀛樺偍榪囩▼榪斿洖涓涓兼椂錛屼綘蹇呴』浣跨敤registerOutParameter鏂規硶鍛婅瘔JDBC椹卞姩鍣ㄨ鍊肩殑SQL綾誨瀷鏄粈涔堛備綘涔熷繀欏昏皟鏁村瓨鍌ㄨ繃紼嬭皟鐢ㄦ潵鎸囩ず璇ヨ繃紼嬭繑鍥炰竴涓箋?
涓嬮潰鎺ョ潃涓婇潰鐨勪緥瀛愩傝繖嬈℃垜浠煡璇ylan Thomas閫濅笘鏃剁殑騫撮緞銆傝繖嬈$殑瀛樺偍榪囩▼浣跨敤PostgreSQL鐨刾l/pgsql錛?

create function snuffed_it_when (VARCHAR) returns integer ''declare

poet_id NUMBER;

poet_age NUMBER;

begin

--first get the id associated with the poet.

SELECT id INTO poet_id FROM poets WHERE name = $1;

--get and return the age.

SELECT age INTO poet_age FROM deaths WHERE mort_id = poet_id;

return age;

end;'' language ''pl/pgsql'';

鍙﹀錛屾敞鎰弍l/pgsql鍙傛暟鍚嶉氳繃Unix鍜孌OS鑴氭湰鐨?n璇硶寮曠敤銆傚悓鏃訛紝涔熸敞鎰忓祵鍏ョ殑娉ㄩ噴錛岃繖鏄拰Java浠g爜鐩告瘮鐨勫彟涓涓紭瓚婃с傚湪Java涓啓榪欐牱鐨勬敞閲婂綋鐒舵槸鍙互鐨勶紝浣嗘槸鐪嬭搗鏉ュ緢鍑屼貢錛屽茍涓斿拰SQL璇彞鑴辮妭錛屽繀欏誨祵鍏ュ埌Java String涓?
涓嬮潰鏄皟鐢ㄨ繖涓瓨鍌ㄨ繃紼嬬殑Java浠g爜錛?

connection.setAutoCommit(false);

CallableStatement proc = connection.prepareCall("{ ? = call snuffed_it_when(?) }");

proc.registerOutParameter(1, Types.INTEGER);

proc.setString(2, poetName);

cs.execute();

int age = proc.getInt(2);

濡傛灉鎸囧畾浜嗛敊璇殑榪斿洖鍊肩被鍨嬩細鎬庢牱錛熼偅涔堬紝褰撹皟鐢ㄥ瓨鍌ㄨ繃紼嬫椂灝嗘姏鍑轟竴涓猂untimeException錛屾濡備綘鍦≧esultSet鎿嶄綔涓嬌鐢ㄤ簡涓涓敊璇殑綾誨瀷鎵紕板埌鐨勪竴鏍楓?

澶嶆潅鐨勮繑鍥炲?

鍏充簬瀛樺偍榪囩▼鐨勭煡璇嗭紝寰堝浜哄ソ鍍忓氨鐔熸倝鎴戜滑鎵璁ㄨ鐨勮繖浜涖傚鏋滆繖鏄瓨鍌ㄨ繃紼嬬殑鍏ㄩ儴鍔熻兘錛岄偅涔堝瓨鍌ㄨ繃紼嬪氨涓嶆槸鍏跺畠榪滅▼鎵ц鏈哄埗鐨勬浛鎹㈡柟妗堜簡銆傚瓨鍌ㄨ繃紼嬬殑鍔熻兘姣旇繖寮哄ぇ寰楀銆?
褰撲綘鎵ц涓涓猄QL鏌ヨ鏃訛紝DBMS鍒涘緩涓涓彨鍋歝ursor錛堟父鏍囷級鐨勬暟鎹簱瀵硅薄錛岀敤浜庡湪榪斿洖緇撴灉涓凱浠f瘡涓琛屻俁esultSet鏄綋鍓嶆椂闂寸偣鐨勬父鏍囩殑涓涓〃紺恒傝繖灝辨槸涓轟粈涔堟病鏈夌紦瀛樻垨鑰呯壒瀹氭暟鎹簱鐨勬敮鎸侊紝浣犲彧鑳藉湪ResultSet涓悜鍓嶇Щ鍔ㄣ?
鏌愪簺DBMS鍏佽浠庡瓨鍌ㄨ繃紼嬩腑榪斿洖娓告爣鐨勪竴涓紩鐢ㄣ侸DBC騫朵笉鏀寔榪欎釜鍔熻兘錛屼絾鏄疧racle銆丳ostgreSQL鍜孌B2鐨凧DBC椹卞姩鍣ㄩ兘鏀寔鍦≧esultSet涓婃墦寮鍒版父鏍囩殑鎸囬拡錛坧ointer錛夈?
璁炬兂鍒楀嚭鎵鏈夋病鏈夋椿鍒伴浼戝勾榫勭殑璇椾漢錛屼笅闈㈡槸瀹屾垚榪欎釜鍔熻兘鐨勫瓨鍌ㄨ繃紼嬶紝榪斿洖涓涓墦寮鐨勬父鏍囷紝鍚屾牱涔熶嬌鐢≒ostgreSQL鐨刾l/pgsql璇█錛?

create procedure list_early_deaths () return refcursor as ''declare

toesup refcursor;

begin

open toesup for SELECT poets.name, deaths.age FROM poets, deaths -- all entries in deaths are for poets. -- but the table might become generic.

WHERE poets.id = deaths.mort_id AND deaths.age < 60;

return toesup;

end;'' language ''plpgsql'';

涓嬮潰鏄皟鐢ㄨ瀛樺偍榪囩▼鐨凧ava鏂規硶錛屽皢緇撴灉杈撳嚭鍒癙rintWriter錛?
PrintWriter:

static void sendEarlyDeaths(PrintWriter out){

Connection con = null;

CallableStatement toesUp = null;

try {

con = ConnectionPool.getConnection();

// PostgreSQL needs a transaction to do this... con.

setAutoCommit(false); // Setup the call.

CallableStatement toesUp = connection.prepareCall("{ ? = call list_early_deaths () }");

toesUp.registerOutParameter(1, Types.OTHER);

toesUp.execute();

ResultSet rs = (ResultSet) toesUp.getObject(1);

while (rs.next()) {

String name = rs.getString(1);

int age = rs.getInt(2);

out.println(name + " was " + age + " years old.");

}

rs.close();

}

catch (SQLException e) { // We should protect these calls. toesUp.close(); con.close();

}

}

鍥犱負JDBC騫朵笉鐩存帴鏀寔浠庡瓨鍌ㄨ繃紼嬩腑榪斿洖娓告爣錛屾垜浠嬌鐢═ypes.OTHER鏉ユ寚紺哄瓨鍌ㄨ繃紼嬬殑榪斿洖綾誨瀷錛岀劧鍚庤皟鐢╣etObject()鏂規硶騫跺榪斿洖鍊艱繘琛屽己鍒剁被鍨嬭漿鎹€?
榪欎釜璋冪敤瀛樺偍榪囩▼鐨凧ava鏂規硶鏄痬apping鐨勪竴涓ソ渚嬪瓙銆侻apping鏄涓涓泦涓婄殑鎿嶄綔榪涜鎶借薄鐨勬柟娉曘備笉鏄湪榪欎釜榪囩▼涓婅繑鍥炰竴涓泦錛屾垜浠彲浠ユ妸鎿嶄綔浼犻佽繘鍘繪墽琛屻傛湰渚嬩腑錛屾搷浣滃氨鏄妸ResultSet鎵撳嵃鍒頒竴涓緭鍑烘祦銆傝繖鏄竴涓煎緱涓句緥鐨勫緢甯哥敤鐨勪緥瀛愶紝涓嬮潰鏄皟鐢ㄥ悓涓涓瓨鍌ㄨ繃紼嬬殑鍙﹀涓涓柟娉曞疄鐜幫細

public class ProcessPoetDeaths{

public abstract void sendDeath(String name, int age);

}

static void mapEarlyDeaths(ProcessPoetDeaths mapper){

Connection con = null;

CallableStatement toesUp = null;

try {

con = ConnectionPool.getConnection();

con.setAutoCommit(false);

CallableStatement toesUp = connection.prepareCall("{ ? = call list_early_deaths () }");

toesUp.registerOutParameter(1, Types.OTHER);

toesUp.execute();

ResultSet rs = (ResultSet) toesUp.getObject(1);

while (rs.next()) {

String name = rs.getString(1);

int age = rs.getInt(2);

mapper.sendDeath(name, age);

}

rs.close();

} catch (SQLException e) { // We should protect these calls. toesUp.close();

con.close();

}

}

榪欏厑璁稿湪ResultSet鏁版嵁涓婃墽琛屼換鎰忕殑澶勭悊錛岃屼笉闇瑕佹敼鍙樻垨鑰呭鍒惰幏鍙朢esultSet鐨勬柟娉曪細

static void sendEarlyDeaths(final PrintWriter out){

ProcessPoetDeaths myMapper = new ProcessPoetDeaths() {

public void sendDeath(String name, int age) {

out.println(name + " was " + age + " years old.");

}

};

mapEarlyDeaths(myMapper);

}

榪欎釜鏂規硶浣跨敤ProcessPoetDeaths鐨勪竴涓尶鍚嶅疄渚嬭皟鐢╩apEarlyDeaths銆傝瀹炰緥鎷ユ湁sendDeath鏂規硶鐨勪竴涓疄鐜幫紝鍜屾垜浠笂闈㈢殑渚嬪瓙涓鏍風殑鏂瑰紡鎶婄粨鏋滃啓鍏ュ埌杈撳嚭嫻併傚綋鐒訛紝榪欎釜鎶宸у茍涓嶆槸瀛樺偍榪囩▼鐗規湁鐨勶紝浣嗘槸鍜屽瓨鍌ㄨ繃紼嬩腑榪斿洖鐨凴esultSet緇撳悎浣跨敤錛屾槸涓涓潪甯稿己澶х殑宸ュ叿銆?

緇撹

瀛樺偍榪囩▼鍙互甯姪浣犲湪浠g爜涓垎紱婚昏緫錛岃繖鍩烘湰涓婃繪槸鏈夌泭鐨勩傝繖涓垎紱葷殑濂藉鏈夛細
&#8226; 蹇熷垱寤哄簲鐢紝浣跨敤鍜屽簲鐢ㄤ竴璧鋒敼鍙樺拰鏀瑰杽鐨勬暟鎹簱妯″紡銆?
&#8226; 鏁版嵁搴撴ā寮忓彲浠ュ湪浠ュ悗鏀瑰彉鑰屼笉褰卞搷Java瀵硅薄錛屽綋鎴戜滑瀹屾垚搴旂敤鍚庯紝鍙互閲嶆柊璁捐鏇村ソ鐨勬ā寮忋?
&#8226; 瀛樺偍榪囩▼閫氳繃鏇村ソ鐨凷QL宓屽叆浣垮緱澶嶆潅鐨凷QL鏇村鏄撶悊瑙c?
&#8226; 緙栧啓瀛樺偍榪囩▼姣斿湪Java涓紪鍐欏祵鍏ョ殑SQL鎷ユ湁鏇村ソ鐨勫伐鍏鳳紞錛嶅ぇ閮ㄥ垎緙栬緫鍣ㄩ兘鎻愪緵璇硶楂樹寒錛?
&#8226; 瀛樺偍榪囩▼鍙互鍦ㄤ換浣昐QL鍛戒護琛屼腑嫻嬭瘯錛岃繖浣垮緱璋冭瘯鏇村姞瀹規槗銆?

騫朵笉鏄墍鏈夌殑鏁版嵁搴撻兘鏀寔瀛樺偍榪囩▼錛屼絾鏄瓨鍦ㄨ澶氬緢媯掔殑瀹炵幇錛屽寘鎷厤璐?寮婧愮殑鍜岄潪鍏嶈垂鐨勶紝鎵浠ョЩ妞嶅茍涓嶆槸涓涓棶棰樸侽racle銆丳ostgreSQL鍜孌B2閮芥湁綾諱技鐨勫瓨鍌ㄨ繃紼嬭璦錛屽茍涓旀湁鍦ㄧ嚎鐨勭ぞ鍖哄緢濂藉湴鏀寔銆?
瀛樺偍榪囩▼宸ュ叿寰堝錛屾湁鍍廡OAD鎴朤ORA榪欐牱鐨勭紪杈戝櫒銆佽皟璇曞櫒鍜孖DE錛屾彁渚涗簡緙栧啓銆佺淮鎶L/SQL鎴杙l/pgsql鐨勫己澶х殑鐜銆?
瀛樺偍榪囩▼紜疄澧炲姞浜嗕綘鐨勪唬鐮佺殑寮閿錛屼絾鏄畠浠拰澶у鏁扮殑搴旂敤鏈嶅姟鍣ㄧ浉姣旓紝寮閿灝忓緱澶氥傚鏋滀綘鐨勪唬鐮佸鏉傚埌闇瑕佷嬌鐢―BMS錛屾垜寤鴻鏁翠釜閲囩敤瀛樺偍榪囩▼鐨勬柟寮忋?/p>

搴鋒枃 2006-10-11 14:37 鍙戣〃璇勮
]]>
OpenSessionInViewFilter瑙e喅Web搴旂敤紼嬪簭鐨勯棶棰?杞嚜錛歅otain 鐨凚LOGhttp://www.tkk7.com/kevinfriend/archive/2006/09/26/71920.html搴鋒枃搴鋒枃Tue, 26 Sep 2006 03:01:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/09/26/71920.htmlhttp://www.tkk7.com/kevinfriend/comments/71920.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/09/26/71920.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/71920.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/71920.html

OpenSessionInView

Created by potian. Last edited by admin 61 days ago. Viewed 181 times.
[edit] [attach]
Hibernate鐨凩azy鍒濆鍖?:n鍏崇郴鏃訛紝浣犲繀欏諱繚璇佹槸鍦ㄥ悓涓涓猄ession鍐呴儴浣跨敤榪欎釜鍏崇郴闆嗗悎錛屼笉鐒禜iernate灝嗘姏鍑轟緥澶栥?

鍙﹀錛屼綘涓嶆効鎰忎綘鐨凞AO嫻嬭瘯浠g爜姣忔閮芥墦寮鍏崇郴Session錛屽洜姝わ紝鎴戜滑涓鑸細閲囩敤OpenSessionInView妯″紡銆?

OpenSessionInViewFilter瑙e喅Web搴旂敤紼嬪簭鐨勯棶棰?

濡傛灉紼嬪簭鏄湪姝e父鐨刉eb紼嬪簭涓繍琛岋紝閭d箞Spring鐨?b style="COLOR: black; BACKGROUND-COLOR: #ffff66">OpenSessionInViewFilter鑳藉瑙e喅闂錛屽畠錛?

protected void doFilterInternal(HttpServletRequest request, 
             HttpServletResponse response,
	     FilterChain filterChain) throws ServletException, IOException {
	SessionFactory sessionFactory = lookupSessionFactory();
	logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
	Session session = getSession(sessionFactory);
	TransactionSynchronizationManager.bindResource(sessionFactory, 
             new SessionHolder(session));
	try {
		filterChain.doFilter(request, response);
	}
	finally {
		TransactionSynchronizationManager.unbindResource(sessionFactory);
		logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
		closeSession(session, sessionFactory);
	}
}
鍙互鐪嬪埌錛岃繖涓狥ilter鍦╮equest寮濮嬩箣鍓嶏紝鎶妔essionFactory緇戝畾鍒癟ransactionSynchronizationManager錛屽拰榪欎釜SessionHolder鐩稿叧銆傝繖涓剰鍛崇潃鎵鏈塺equest鎵ц榪囩▼涓皢浣跨敤榪欎釜session銆傝屽湪璇鋒眰緇撴潫鍚庯紝灝嗗拰榪欎釜sessionFactory瀵瑰簲鐨剆ession瑙g粦錛屽茍涓斿叧闂璖ession銆?

涓轟粈涔堢粦瀹氫互鍚庯紝灝卞彲浠ラ槻姝㈡瘡嬈′笉浼氭柊寮涓涓猄ession鍛紵鐪嬬湅HibernateDaoSupport鐨勬儏鍐碉細

publicfinal void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
  }
 protectedfinal HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }

鎴戜滑鐨凞AO灝嗕嬌鐢ㄨ繖涓猼emplate榪涜鎿嶄綔錛?

publicabstract class BaseHibernateObjectDao
	extends HibernateDaoSupport
	implements BaseObjectDao {

protected BaseEntityObject getByClassId(finallong id) { BaseEntityObject obj = (BaseEntityObject) getHibernateTemplate() .execute(new HibernateCallback() {

publicObject doInHibernate(Session session) throws HibernateException { return session.get(getPersistentClass(), newLong(id)); }

}); return obj; }

public void save(BaseEntityObject entity) { getHibernateTemplate().saveOrUpdate(entity); }

public void remove(BaseEntityObject entity) { try {

getHibernateTemplate().delete(entity); } catch (Exception e) { thrownew FlexEnterpriseDataAccessException(e); } }

public void refresh(final BaseEntityObject entity) { getHibernateTemplate().execute(new HibernateCallback() {

publicObject doInHibernate(Session session) throws HibernateException { session.refresh(entity); returnnull; }

}); }

public void replicate(finalObject entity) { getHibernateTemplate().execute(new HibernateCallback() {

publicObject doInHibernate(Session session) throws HibernateException { session.replicate(entity, ReplicationMode.OVERWRITE); returnnull; }

}); }

鑰孒ibernateTemplate璇曞浘姣忔鍦╡xecute涔嬪墠鍘昏幏寰桽ession錛屾墽琛屽畬灝卞姏浜夊叧闂璖ession
publicObject execute(HibernateCallback action) throws DataAccessException {
	Session session = (!this.allowCreate ?
		SessionFactoryUtils.getSession(getSessionFactory(), 
                  false) :
		SessionFactoryUtils.getSession(getSessionFactory(),
                  getEntityInterceptor(),
                  getJdbcExceptionTranslator()));
	boolean existingTransaction =  
          TransactionSynchronizationManager.hasResource(getSessionFactory());
	if (!existingTransaction && getFlushMode() == FLUSH_NEVER) {
		session.setFlushMode(FlushMode.NEVER);
	}
	try {
		Object result = action.doInHibernate(session);
		flushIfNecessary(session, existingTransaction);
		return result;
	}
	catch (HibernateException ex) {
		throw convertHibernateAccessException(ex);
	}
	catch (SQLException ex) {
		throw convertJdbcAccessException(ex);
	}
	catch (RuntimeException ex) {
		// callback code threw application exception
		throw ex;
	}
	finally {
		SessionFactoryUtils.closeSessionIfNecessary(
                    session, getSessionFactory());
	}
}
鑰岃繖涓猄essionFactoryUtils鑳藉惁寰楀埌褰撳墠鐨剆ession浠ュ強closeSessionIfNecessary鏄惁鐪熸鍏抽棴session錛岀鍙栧喅浜庤繖涓猻ession鏄惁鐢╯essionHolder鍜岃繖涓猻essionFactory鍦ㄦ垜浠渶寮濮嬫彁鍒扮殑TransactionSynchronizationManager緇戝畾銆?
publicstatic void closeSessionIfNecessary(Session session, 
    SessionFactory sessionFactory)   
    throws CleanupFailureDataAccessException {
	if (session == null || 
	   TransactionSynchronizationManager.hasResource(sessionFactory)) {
		return;
	}
	logger.debug("Closing Hibernate session");
	try {
		session.close();
	}
	catch (JDBCException ex) {
		// SQLException underneath
		thrownew CleanupFailureDataAccessException(
		"Cannot close Hibernate session", ex.getSQLException());
	}
	catch (HibernateException ex) {
		thrownew CleanupFailureDataAccessException(
		"Cannot close Hibernate session", ex);
	}
}

HibernateInterceptor鍜孫penSessionInViewInterceptor鐨勯棶棰?

浣跨敤鍚屾牱鐨勬柟娉曪紝榪欎袱涓狪nterceptor鍙互鐢ㄦ潵瑙e喅闂銆備絾鏄叧閿殑涓嶅悓涔嬪鍦ㄤ簬錛屽畠浠殑鍔涘害鍙兘瀹氫箟鍦―AO鎴栦笟鍔℃柟娉曚笂錛岃屼笉鏄湪鎴戜滑鐨凾est鏂規硶涓婏紝闄ら潪鎴戜滑鎶婂畠浠簲鐢ㄥ埌TestCase鐨勬柟娉曚笂錛屼絾浣犱笉澶у彲鑳戒負TestCase鍘誨畾涔変竴涓帴鍙o紝鐒跺悗鎶奍nterceptor搴旂敤鍒拌繖涓帴鍙g殑鏌愪簺鏂規硶涓娿傜洿鎺ヤ嬌鐢℉ibernateTransactionManager涔熸槸涓鏍風殑銆傚洜姝わ紝濡傛灉鎴戜滑鏈夎繖鏍風殑嫻嬭瘯錛?

Category parentCategory  = new Category ();
	parentCategory.setName("parent");
	dao.save(parentCategory);

Category childCategory = new Category(); childCategory.setName("child");

parentCategory.addChild(childCategory); dao.save(childCategory);

Category savedParent = dao.getCategory("parent"); Category savedChild = (Category ) savedParent.getChildren().get(0); assertEquals(savedChild, childCategory);

灝嗘剰鍛崇潃涓や歡浜嬫儏錛?
  • 姣忔DAO鎵ц閮戒細鍚姩涓涓猻ession鍜屽叧闂竴涓猻ession
  • 濡傛灉鎴戜滑瀹氫箟浜嗕竴涓猯azy鐨勫叧緋伙紝閭d箞鏈鍚庣殑Category savedChild = (Category ) savedParent.getChildren().get(0);灝嗕細璁﹉ibernate鎶ラ敊銆?

瑙e喅鏂規

涓縐嶆柟娉曟槸瀵筎estCase搴旂敤Interceptor鎴栬匱ransactionManager錛屼絾榪欎釜鎭愭曚細閫犳垚寰堝楹葷儲銆傞櫎闈炴槸浣跨敤澧炲己鏂瑰紡鐨凙OP.鎴戝墠鏈熼噰鐢ㄨ繖縐嶆柟娉?Aspectwerkz)錛屽湪Eclipse閲岄潰涔熻窇寰楀惈濂姐?

鍙︿竴縐嶆柟娉曟槸鍦═estCase鐨剆etup鍜宼eardown閲岄潰瀹炵幇鍜孎ilter瀹屽叏涓鏍風殑澶勭悊錛屽叾浠栫殑TestCase閮戒粠榪欎釜TestCase緇ф壙錛岃繖縐嶆柟娉曟槸鎴戠洰鍓嶆墍浣跨敤鐨勩?

Jolestar琛ュ厖:openSessionInView鐨勯厤緗柟娉?

聽聽 <filter>
聽 聽 聽 聽 <filter-name>opensession</filter-name>
聽 聽 聽 聽 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
聽 聽 聽 聽 <init-param>
聽 聽 聽 聽 聽 聽 <param-name>singleSession</param-name>
聽 聽 聽 聽 聽 聽 <param-value>false</param-value>
聽 聽 聽 聽 </init-param>
聽 聽 </filter>



搴鋒枃 2006-09-26 11:01 鍙戣〃璇勮
]]>
Spring -transactionhttp://www.tkk7.com/kevinfriend/archive/2006/09/15/69830.html搴鋒枃搴鋒枃Fri, 15 Sep 2006 03:00:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/09/15/69830.htmlhttp://www.tkk7.com/kevinfriend/comments/69830.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/09/15/69830.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/69830.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/69830.html1 Understand Transaction
聽 1) Introduce Spring's transaction manager
聽 a聽 JDBC transactions聽
聽聽聽聽 <bean id="transactionManager" class="org.springframework.jdbc.
聽聽聽聽聽 datasource.DataSourceTransactionManager">
聽聽聽聽 <property name="dataSource">
聽聽聽聽 <ref bean="dataSource"/>
聽聽聽聽 </property>
聽聽聽聽 </bean>
聽聽 b Hibernate transactions
聽聽聽聽 <bean id="transactionManager" class="org.springframework.
聽聽聽聽聽聽 orm.hibernate.HibernateTransactionManager">
聽聽聽聽 <property name="sessionFactory">
聽聽聽聽 <ref bean="sessionFactory"/>
聽聽聽聽 </property>
聽聽聽聽 </bean>
2 Programing transaction in Spring
聽聽 One approach to adding transaction to your code is to programmly add transactional boundary using transiationTemplate class.
聽聽 Programming is good when you want complete control over transactional boundary.but you have to use spring specific class.In most case ,your tansactional needs will not require such precise control over transactional boundaries.That is why you will typically choolse to declare transaction support
聽 public void enrollStudentInCourse() {
聽聽聽 transactionTemplate.execute(
聽聽聽 new TransactionCallback() {
聽聽聽聽聽 public Object doInTransaction(TransactionStatus ts) {
聽聽聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽聽 // do stuff聽聽 Runs within doInTransaction()
聽聽聽聽聽聽聽 } catch (Exception e) {
聽聽聽聽聽聽聽聽聽 ts.setRollbackOnly(); //Calls setRollbackOnly() to roll Calls setRollbackOnly()聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //to roll back
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽聽聽 return null;聽聽 //If successful, transaction is committed
聽聽聽聽聽 }
聽聽聽 }
聽聽 );
聽}
聽<bean id="transactionTemplate" class="org.springframework.
聽聽聽聽聽聽 transaction.support.TransactionTemplate">
聽 <property name="transactionManager">
聽聽聽 <ref bean="transactionManager"/>
聽 </property>
聽</bean>
聽<bean id="courseService"
聽聽聽 class="com.springinaction.training.service.CourseServiceImpl">
聽<property name=" transactionTemplate">
聽聽聽聽 <ref bean=" transactionTemplate"/>
聽聽 </property>
聽</bean>
3 Declaring transactions
聽 Spring's support for declarative transaction management is implementedd through Spirng's聽 AOP framework.
聽 <bean id="courseService" class="org.springframework.transaction.
聽聽聽聽聽聽 interceptor.TransactionProxyFactoryBean">
聽 <property name="proxyInterfaces">
聽聽聽 <list>
聽聽聽聽聽 <value>
聽聽聽聽聽聽聽 com.springinaction.training.service.CourseService聽聽
聽聽聽聽聽 </value>
聽聽聽 </list>
聽 </property>
聽 <property name="target">
聽聽 <ref bean="courseServiceTarget"/>聽聽 //Bean being proxied
聽 </property>
聽<property name="transactionManager">
聽聽 <ref bean="transactionManager"/>聽聽 //Transaction manager
聽</property>
聽<property name="transactionAttributeSource">
聽聽 <ref bean="attributeSource"/>聽聽 //Transaction attribute source
聽</property>
聽</bean>
聽1) Understanding transaction attributes
聽 In Spring transaction attribute is a description of how transaction policies should be
applied to a methods
聽聽 a聽 Propagation behavior
聽聽聽 Propagation behavior聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 What it means
聽聽聽 PROPAGATION_MANDATORY聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 indicate that the method must run within a聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction.If no transaction is in progress
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 an exception will be thrown
聽聽聽 PROPAGATION_NESTED
聽聽聽 PROPAGATION_NEVER聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 indicate that the method can not run withi a聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction. if a transaction exist an exception聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 will be thrown.
聽聽聽 PROPAGATIOM_NOT_SUPPORT聽聽聽聽聽聽聽聽聽聽聽聽聽 Indicates that the method should not run within a聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction. If an existing transaction is聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 in progress, it will be suspended for the
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 duration of the method.
聽聽聽 PROPAGATION_REQUIRED聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 indicate that the current method must run within a聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction.if an existing transaction is in聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 progress,the ,method will run with the transaction
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 otherwise a new transaction will be started
聽聽聽 PROPAGATION_REQUIRENEW聽聽聽聽聽聽聽聽聽聽聽聽聽聽 indicates that the current must run within its own
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction.A new transaction is started and an聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 existing transaction will be suspend
聽聽聽 PROPAGATION_SUPPORT聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 indicate the current mehtod does not require a聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 transaction.but may run if on is already in progress聽聽聽聽聽
聽聽聽 b Isolation levels聽聽聽聽聽
聽聽聽 Isolation level聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 What it means
聽聽聽 ISOLATION_DEFAULT聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Using the defaul isolation level of the underlying聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 database
聽聽聽 ISOLATION_READ_UNCOMMITTED聽聽聽聽聽聽聽聽 Allows you read change that have not yet been commit
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 May result in dirty read,phantom read,nonrepeatable聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 read
聽聽聽 ISOLATION_READ_COMMITTED聽聽聽聽聽聽聽聽聽聽 Allows reads from concurrent transactions that have
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 bean committed.Dirty read are prevent.but platform聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 and norepeatable reads may still occur.
聽聽聽 ISOLATIOM_REPEATABLE_READ聽聽聽聽聽聽聽聽聽 Multiple read the same field will yield the same聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 result ,unless changed by the transaction聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 itself.Dirty reads ,nonrepeatable are all prevented
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 phantom may still occur
聽聽聽 ISOLATION_SERIALIZABLE聽聽聽聽聽聽聽聽聽聽聽聽 This fully ACID-compliant isolation level ensusme聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 that dirty read,unrepeatable read ,phantom read are聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 all prevented.And this is the most slowest isolation
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 since it is typically accomplished by doing full聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 table lock on the tables in the transaction.

聽聽 c Read-Only
聽聽 If a transaction performs only read operation against the underlying datastore.when a transaction begin ,it only make sense to declare a transaction as read only on mehtods with
propagation behavior which start a new transaction.
聽聽 Furthermore ,if you are Hibernate as persistence mechanism,declaring a transaction as read only will reult in Hibernate flush mode being set to FLUST_NEVER.this tell hibernate to avoid synchroniztion of objects with database.
聽聽 d Transaction timeout
聽聽 Suppose that your transaction becomes unexpectedly long-running transaction.Because transaction may invole locks on the underlying database.Instead of waiting it out ,you can delcare a transaction to automaitically roll back.
聽聽 because timeout clock begin ticking when a transaction start. it only make sense to declare a transaction timeout on methods with propagation behavior that start a new transaction.
聽 2) Declaring a simple transaction policy
聽聽 <bean id="myTransactionAttribute"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 DefaultTransactionAttribute">
聽 <property name="propagationBehaviorName">
聽聽聽 <value>PROPAGATION_REQUIRES_NEW</value>
聽 </property>
聽 <property name="isolationLevelName">
聽聽聽 <value>ISOLATION_REPEATABLE_READ</value>
聽 </property>
聽</bean>
聽<bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 MatchAlwaysTransactionAttributeSource">
聽 <property name="transactionAttribute">
聽聽聽 <ref bean="myTransactionAttribute"/>
聽 </property>
聽</bean>
4 Declaring transactions by method name
聽 1) Using NameMatchTransactionAttributeSource
聽 The properties property of NameMatchTransactionAttributeSource maps mehtod to a transaction property descriptor. the property descriptor takes the following form:
聽 Propagation,isolation,readOnly,-Exception,+Exception

聽 <bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 NameMatchTransactionAttributeSource">
聽 <property name="properties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="enrollStudentInCourse">
聽聽聽聽聽聽聽聽聽 PROPAGATION_REQUIRES_NEW
聽聽聽聽聽 </prop>
聽聽聽 </props>
聽 </property>
聽</bean>
聽2) Specifying the transaction Isolation level
聽 <bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 NameMatchTransactionAttributeSource">
聽聽聽聽 <property name="properties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="enrollStudentInCourse">
聽聽聽聽聽聽聽 PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ
聽聽聽聽聽 </prop>
聽聽聽 </props>
聽 </property>
聽</bean>
聽3) Using real-only transaction
聽 <bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 NameMatchTransactionAttributeSource">
聽 <property name="properties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="getCompletedCourses">
聽聽聽聽聽聽聽 PROPAGATION_REQUIRED,ISOLATION_REPEATABLE_READ,readOnly
聽聽聽聽聽 </prop>
聽聽聽 </props>
聽 </property>
</bean>
聽4)Specifying聽 rollback rules
聽 You can sepcify that a transaction be rollback on specify checked exception
聽 <bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 NameMatchTransactionAttributeSource">
聽 <property name="properties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="enrollStudentInCourse">
聽聽聽聽聽聽聽 PROPAGATION_REQUIRES_NEW,ISOLATION_REPEATABLE_READ,
聽聽聽聽聽聽聽 -CourseException
聽聽聽聽聽聽聽 </prop>
聽聽聽聽 </props>
聽聽聽 </property>
聽 </bean>
聽 Exception can be marked as negative(-) or postive(+)
聽 Negative exception will trigger the roll back if the exception (or sublclass of it) is thrown.Postive exception on the other hand indicate that the transacton should be commit
even if the exception is thrown
聽5)Using wildcard matches
聽<bean id="transactionAttributeSource"
聽聽聽 class="org.springframework.transaction.interceptor.
聽聽聽聽聽聽聽聽聽聽聽 NameMatchTransactionAttributeSource">
聽 <property name="properties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="get*">
聽聽聽聽聽聽聽 PROPAGATION_SUPPORTS
聽聽聽聽聽 </prop>
聽聽聽 </props>
聽 </property>
</bean>
聽6 Short-cut name match transaction
聽<bean id="courseService" class="org.springframework.transaction.
聽聽聽聽聽聽 interceptor.TransactionProxyFactoryBean">
聽聽 <property name="transactionProperties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="enrollStudentInCourse">
聽聽聽聽聽聽聽 PROPAGATION_REQUIRES_NEW
聽聽聽聽聽 </prop>
聽聽聽 </props>
聽聽 </property>
聽</bean>



搴鋒枃 2006-09-15 11:00 鍙戣〃璇勮
]]>
spring -databasehttp://www.tkk7.com/kevinfriend/archive/2006/09/14/69598.html搴鋒枃搴鋒枃Thu, 14 Sep 2006 03:45:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/09/14/69598.htmlhttp://www.tkk7.com/kevinfriend/comments/69598.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/09/14/69598.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/69598.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/69598.html涓 Spring DAO philosophy
聽1 Understanding Spring's DataAccesssException
聽Spring's DAO frameworks donot throw teechnology-specific exceptions such as SQLException
or HibernateeExcepiton.Instead ,all exceptions thrown are subclasses of DataAccessException
聽2 You are not forced to handle DataAccessExceptions
聽DataAccessException is a RuntimeException,so it si an unchecked exception.Since these are quite often unrecoverable,you are not forced to handle these exception.
聽 Instead ,you can catch the exception if recovery is possible.since DataAccessException is not only a RuntimeException,but it subclasses Spring's NestedRuntimeException. This menas that the root Exception is alwarys via NestedRuntimeException's getCause() method.
聽3 Work with DataSources
聽 a getting a Datasource from JNDI
聽聽 <bean id="dataSource"
聽聽聽聽聽 class="org.springframework.jndi.JndiObjectFactoryBean">
聽聽 <property name="jndiName">
聽聽聽 <value>java:comp/env/jdbc/myDatasource</value>
聽聽 </property>
聽聽 </bean>
聽 b Creating a Datasource connection pool
聽聽 <bean id="dataSource"
聽聽聽聽聽 class="org.apache.commons.dbcp.BasicDataSource">
聽 <property name="driver">
聽聽聽 <value>${db.driver}</value>
聽 </property>
聽 <property name="url">
聽聽聽 <value>${db.url}</value>
聽 </property>
聽 <property name="username">
聽聽聽 <value>${db.username}</value>
聽 </property>
聽 <property name="password">
聽聽聽 <value>${db.password}</value>
聽 </property>
</bean>
聽c Using a DataSource while testing
聽 DriverManagerDataSource dataSource = new DriverManagerDataSource();
聽 dataSource.setDriverClassName(driver);
聽 dataSource.setUrl(url);
聽 dataSource.setUsername(username);
聽 dataSource.setPassword(password);
聽4 Consistent DAO support
聽 Spring template class handle the invariant part of data access-controling the trancsaction
manage resource,handling exception .Implementation of callback interface define what is specific to your application--creating statement,binding parameter and marshalling result set.
聽Spring separates the fixed an vaiant parts of data access process into tow distince classes:
template and callbacks.Template manage the fixed parts of the process while callback are where you fill in the implement details;
聽one the top of template-callback desing ,spring framework provide a support class which your own data access subclass it. And the support class already have a property for holding a template.
聽浜?Integerating Hibernate with Spring
聽 1 Managing Hibernate resources
聽聽 you will keep a single instance of SessionFactory throughtout your application
聽聽 <bean id="sessionFactory"class="org.springframework.
聽聽聽聽聽聽聽聽聽聽 orm.hibernate.LocalSessionFactoryBean">
聽聽聽聽 <bean id="sessionFactory" class="org.springframework.
聽聽聽聽聽聽 orm.hibernate.LocalSessionFactoryBean">
聽聽聽聽 <property name="dataSource">
聽聽聽聽聽聽 <ref bean="dataSource"/>
聽聽聽聽 </property>
聽 </bean>
聽 you also want to manager how hibernate is configured
聽 <bean id="sessionFactory" class="org.springframework.
聽聽聽聽聽聽 orm.hibernate.LocalSessionFactoryBean">
聽 <property name="hibernateProperties">
聽聽聽 <props>
聽聽聽聽聽 <prop key="hibernate.dialect">net.sf.hibernate.
聽聽聽聽聽聽聽聽聽聽 dialect.MySQLDialect</prop>

聽聽聽 </props>
聽 </property>
聽 鈥?br />聽</bean>
聽and the last thing is whick map files is read
聽 <bean id="sessionFactory" class="org.springframework.
聽聽聽聽聽聽 orm.hibernate.LocalSessionFactoryBean">
聽 <property name="mappingResources">
聽聽聽聽 <list>
聽聽聽聽 <value>Student.hbm.xml</value>
聽聽聽聽 <value>Course.hbm.xml</value>
聽聽聽聽 鈥?br />聽聽 </list>
聽 </property>
聽聽聽聽 鈥?br />聽 </bean>

聽Now聽 you have fully configured your sessionfactory ,so we need do create an object which we
will access hibernate. As we know, we will use a template class
聽 <bean id="hibernateTemplate"
聽聽聽聽聽 class="org.springframework.orm.hibernate.HibernateTemplate">
聽 <property name="sessionFactory">
聽聽聽 <ref bean="sessionFactory"/>
聽 </property>
聽</bean>

聽 <bean id="courseDao" class="com.springinaction.
聽聽聽聽聽聽 training.dao.hibernate.CourseDaoHibernate">
聽 <property name="hibernateTemplate">
聽聽聽 <ref bean="hibernateTemplate"/>
聽 </property>
聽</bean>

聽2 Accessing Hibernate through HibernatTemplate
聽 The template-callback mechanism in Hibernatee is pretty simple.There is the HibernatTmpplate and one callback interface
聽 public Student getStudent(final Integer id) {
聽 return (Student) hibernateTemplate.execute(
聽聽聽 new HibernateCallback() {
聽聽聽聽聽 public Object doInHibernate(Session session)
聽聽聽聽聽聽聽聽聽 throws HibernateException {
聽聽聽聽聽聽聽 return session.load(Student.class, id);
聽聽聽聽聽 }
聽聽聽 });

聽 The HibernateTemplate class provides some convience methods that implicit create a HibernateCallback instance:
聽 (Student) hibernateTemplate.load(Student.class, id);
聽聽 hibernateTemplate.update(student);
聽 hibernateTemplate.find("from Student student " +
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 "where student.lastName = ?",
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 lastName, Hibernate.STRING);
聽 3 Subclassing HibernateDaoSupport
聽 public class StudentDaoHibernate extends HibernateDaoSupport
聽聽聽 implements StudentDao {
聽 鈥?br />聽 }
聽 getHibernateTemplate()
聽 getSession()



搴鋒枃 2006-09-14 11:45 鍙戣〃璇勮
]]>
hibernate 涓綰х紦瀛?/title><link>http://www.tkk7.com/kevinfriend/archive/2006/09/14/69535.html</link><dc:creator>搴鋒枃</dc:creator><author>搴鋒枃</author><pubDate>Thu, 14 Sep 2006 01:22:00 GMT</pubDate><guid>http://www.tkk7.com/kevinfriend/archive/2006/09/14/69535.html</guid><wfw:comment>http://www.tkk7.com/kevinfriend/comments/69535.html</wfw:comment><comments>http://www.tkk7.com/kevinfriend/archive/2006/09/14/69535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/kevinfriend/comments/commentRss/69535.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/kevinfriend/services/trackbacks/69535.html</trackback:ping><description><![CDATA[ <p>1 hibernate 涓綰х紦瀛?br />Session聽 <br />聽 evict(Object o) 浠庣紦瀛樹腑娓呴櫎鎸囧畾鐨勬寔涔呭寲瀵硅薄<br />聽 clear()聽聽聽聽聽聽聽聽 娓呴櫎緙撳瓨涓墍鏈夊璞?br />2 鎵歸噺鏇存柊浜庢壒閲忓垹闄?br />聽 1) 鎵歸噺鏇存柊<br />聽聽 Iterator customers=session.find("from Customer c where c.age>0");<br />聽聽 while(customers.hasNext()){<br />聽聽聽聽 Customer customer=(Customer)customers.next();<br />聽聽聽聽 customer.setAge(customer.getAge()+1);<br />聽聽 }<br />聽聽 tx.commit();<br />聽聽 session.close();</p> <p>聽 緙虹偣錛氬唴瀛樹腑鍔犺澆浜嗗ぇ閲忔暟鎹?br />聽聽聽聽聽聽聽 鎵ц浜嗗嬈pdate 璇彞<br />聽 <br />聽聽 鏀硅繘<br />聽聽 Iterator customers=session.find("from Customer c where c.age>0");<br />聽聽 while(customers.hasNext()){<br />聽聽聽聽 Customer customer=(Customer)customers.next();<br />聽聽聽聽 customer.setAge(customer.getAge()+1);<br />聽聽聽聽 session.flush();<br />聽聽聽聽 session.evict(customer);<br />聽聽 }<br />聽聽 tx.commit();<br />聽聽 session.close();<br />聽聽 閬楃暀闂<br />聽聽 鎵ц浜嗗嬈pdate 璇彞<br />聽聽 <br />聽聽 閲囩敤jdbc api 榪涜璋冪敤<br />聽聽 Connection con=session.connection();<br />聽聽 PrepareStatement stmt=con.prepareStatement("update customers set age=age+1 where age>0");<br />聽聽 stmt.executeUpdate();<br />聽聽 tx.commit();<br />聽聽 鍙﹀錛屼篃鍙互璋冪敤搴曞眰鐨勫瓨鍌ㄨ繃紼嬭繘琛屾壒閲忔洿鏂?br />聽聽 create or replace procedure batchUpdateCustomer(p_age,in number) as<br />聽聽 begin<br />聽聽聽聽聽 update customer set age=age+1 where age>p_age;<br />聽聽 end; <br />聽聽 <br />聽聽 tx=session.beginTransaction();<br />聽聽 Connection con=session.connection();<br />聽聽 CallableStatement cstmt=con.prepareCall(batchUpdateCustomer);<br />聽聽 cstmt.setInt(1,0);<br />聽聽 cstmt.eqecuteUpdate();<br />聽聽 tx.commit();<br />聽聽 2) 鎵歸噺鏁版嵁鐨勫垹闄?br />聽聽聽 session.delete("from聽 Customer c where c.age>0");<br />聽聽聽 瀹為檯璋冪敤鐨勮繃紼?br />聽聽聽 session * from Customer where age>0;<br />聽聽聽 鍦ㄦ妸鎵鏈夋暟鎹姞杞藉埌鍐呭瓨涔嬪悗鎵ц澶氭潯delete 璇彞<br />聽聽聽 delete from customer where id=i;<br />聽聽聽聽 .......................<br />聽聽 鏀硅繘鍔炴硶閲囩敤jdbc api 榪涜鎵歸噺鏁版嵁鐨勫垹闄?br />聽聽聽聽聽 <br />聽聽 tx=session.beginTransaction();<br />聽聽 Connection con=session.connection();<br />聽聽 con.execute("delete from customers where age>0");<br />聽聽 tx.commit();</p> <img src ="http://www.tkk7.com/kevinfriend/aggbug/69535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/kevinfriend/" target="_blank">搴鋒枃</a> 2006-09-14 09:22 <a href="http://www.tkk7.com/kevinfriend/archive/2006/09/14/69535.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>hibernate -綾誨瀷http://www.tkk7.com/kevinfriend/archive/2006/09/14/69534.html搴鋒枃搴鋒枃Thu, 14 Sep 2006 01:20:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/09/14/69534.htmlhttp://www.tkk7.com/kevinfriend/comments/69534.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/09/14/69534.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/69534.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/69534.htmlHibernate聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 sql聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 oracle聽聽聽聽聽聽聽

integer or int聽聽聽聽聽聽聽聽聽聽聽聽 int or Integer聽聽聽聽聽聽聽聽聽聽 INTEGER
long聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 long or Long聽聽聽聽聽聽聽聽聽聽聽聽 BIGINT
short聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 short or Short聽聽聽聽聽聽聽聽聽聽 SMALLINT
byte聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 byte or Byte聽聽聽聽聽聽聽聽聽聽聽聽 TINYINT
float聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 float or Float聽聽聽聽聽聽聽聽聽聽 FLOAT
double聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 double or Double聽聽聽聽聽聽聽聽 DOUBLE
big_decimal聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java.math.BigDecimal聽聽聽聽 NUMBERBIC
character聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 char java.lang.Character CHAR(1)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
string聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 VARCHAR
boolean聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 boolean or Boolean聽聽聽聽聽聽聽 BIT
date聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java.util.Date聽聽聽聽聽聽聽聽聽聽聽 DATE
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java.sql.Date
time聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Date or java.sql.time聽聽聽聽 TIME
timestamp聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Date or java.sql.Timestamp TIMESTAMP聽聽聽聽聽聽聽聽聽聽聽聽聽聽
binary聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 byte[]聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blog聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blog
text聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 String聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 clob聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 clog
serializable聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blog聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blog聽聽聽聽聽聽聽聽聽聽聽聽
clob聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java.sql.clob聽聽聽聽聽聽聽聽聽聽聽 clob聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 clob
blob聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 java.sql.blob聽聽聽聽聽聽聽聽聽聽聽聽 blog聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blob



搴鋒枃 2006-09-14 09:20 鍙戣〃璇勮
]]>
spring --wiring bindinghttp://www.tkk7.com/kevinfriend/archive/2006/09/04/67531.html搴鋒枃搴鋒枃Mon, 04 Sep 2006 03:10:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/09/04/67531.htmlhttp://www.tkk7.com/kevinfriend/comments/67531.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/09/04/67531.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/67531.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/67531.htmlContaining your beans
聽聽聽 Tere i sno single Spring container.Spring actually comes with two distince types of containers:Bean factories and Application

contexts.Beyong there two basic types of contains .Srping come with sereral implementss of BeanFacotory and ApplicationContext.
聽 1 introducing the BeanFactory
聽 There are several implementations of BeanFactory in Spring .But the most userful one is XmlBeanFactory,whick loads
its bean based on the definitions contained in an xml file.
聽 Creat a XmlBeanFactory聽 BeanFactory factory=new XMLBeanFactory(new FileInputStream("beans.xml"));
聽But at that time ,the BeanFactory does not initialize the bean ,it is loaded lazliy.
聽 Get the Bean :MyBean myBean=(MyBean)factory.getBean("myBean");
聽When getBean() is called ,the factory will instantiate the bean and being setting the bean using dependency injection.
聽 2 Working with an application context
聽an ApplicationContextis prefered over a BeanFactory in nearly all application ,the only time you might consider using a BeanFactory

are in circumtance where resource s are scarce.
聽聽 Amony the many implements of ApplicationContext are three that are commonly used:
聽聽聽聽聽 ClassPathXmlApplicationContext,FileSystemXmpApplicationContext,XmlWebApplicationContext.
聽聽 ApplicationContext context=new ClassPathXmlApplicationContext("foo.xml");
聽wiring the beans
聽 <beans>
聽聽聽 <bean id="foo" class="com.springinaction.Foo" />
聽</beans>
聽Prototyping vs.singleton
聽By default ,all Spring beans are singletons.When the container dispenses a bean it will always give the exact same instance of the聽聽聽聽

In this case ,you would want to define a prototype bean .Defining a prototype means that instead of defining a single bean.
聽聽聽聽 <bean id="foo" class="com.springinaction.Foo"聽 single/>on="false" />
Initialization and destruction
聽聽 <bean id="foo" class="com.springinaction.Foo" init-method="setup" destory-method="teardown">
Injectiong dependencies via setter method
聽聽 <bean id="foo" class="com.srpinginaction.Foo" >
聽聽聽聽聽聽聽 <property name="name">Foo McFoo</value>
聽聽 </bean>
聽Referencing other beans
聽聽 <bean id="foo" class="com.springinaction.Foo">
聽聽聽聽 <property name="bar" >
聽聽聽聽聽聽聽 <ref bean="bar" />
聽聽聽聽 </property>
聽聽聽 </bean>
聽聽聽 <bean id="bar" colass="com.srpinginaction.Bar" />
聽Inner beans
聽聽 <bean id="courseService"
聽聽聽聽聽聽聽聽聽聽聽聽 class="com.CourseWericeImpl">
聽聽聽聽 <property nanme="studentService">
聽聽聽聽聽聽聽聽 <bean
聽聽聽聽聽聽聽聽聽聽聽聽聽 class="com....." />
聽聽聽聽聽 </property>
聽聽 </bean>
聽 Wiring collections
聽聽聽 1Wiring lists and arrays聽 java.util.List
聽聽聽聽聽 <property name="barList">
聽聽聽聽聽聽聽聽聽聽 <list>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <value>bar1</value>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <ref bean="bar2"/>
聽聽聽聽聽聽聽聽聽聽 </lsit>
聽聽聽聽聽 </property>
聽聽 2 Wiring set聽 java.tuil.Set
聽聽聽 <property name="barSet">
聽聽聽聽聽聽聽聽聽 <set>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <value>bar1</value>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <ref bean="bar2" />
聽聽聽聽聽聽聽聽聽 </set>
聽聽 </property>
聽聽 3 Wiring maps java.util.Map
聽聽 <property name="barMap">
聽聽聽聽聽聽聽 <ebtry key="key1">
聽聽聽聽聽聽聽 <value>bar1</value>
聽聽 </property>
聽 4 Wiring propertyies
聽聽聽 <property name="barProps">
聽聽聽聽聽聽聽聽 <props>
聽聽聽聽聽聽聽聽聽聽聽 <prop key="key1">bar1</prop>
聽聽聽聽聽聽聽聽聽聽聽 <prop key="key2">bar2</prop>
聽聽聽聽聽聽聽聽 </props>
聽聽聽 </property>
聽 5 Setting null values
聽聽 <property name="foo"><null/><property>
聽聽 injecting dependencies via constructor
聽聽 <id="foo" class="com.springinaction.Foo">
聽聽聽 <constructor-arg>
聽聽聽聽聽聽聽 <value>42<value>( <ref bean="bar">)
聽聽聽聽 </constructor-arg>
聽聽聽 <bean id="foo" class="com.springinaction.Foo">
聽聽聽聽聽聽 <constructor-arg>
聽聽聽聽聽聽聽聽聽聽聽 <value>http://www.manning.com</value>
聽聽聽聽聽聽 </constructor-arg>
聽 <constructor-arg>
聽聽聽聽聽聽聽聽聽聽聽 <value>http://www.manning.com</value>
聽聽聽聽聽聽 </constructor-arg>
聽聽 </bean>



搴鋒枃 2006-09-04 11:10 鍙戣〃璇勮
]]>
hibernate 鎬葷粨鍩虹http://www.tkk7.com/kevinfriend/archive/2006/08/18/64378.html搴鋒枃搴鋒枃Fri, 18 Aug 2006 08:20:00 GMThttp://www.tkk7.com/kevinfriend/archive/2006/08/18/64378.htmlhttp://www.tkk7.com/kevinfriend/comments/64378.htmlhttp://www.tkk7.com/kevinfriend/archive/2006/08/18/64378.html#Feedback0http://www.tkk7.com/kevinfriend/comments/commentRss/64378.htmlhttp://www.tkk7.com/kevinfriend/services/trackbacks/64378.html聽 java .lang.String聽聽聽聽聽聽 string聽聽聽聽聽聽聽聽聽聽聽聽聽聽 varchar
聽 java.lang.String聽聽聽聽聽聽聽 text聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Text
聽 int聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 int聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 INT
聽 char聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 character聽聽聽聽聽聽聽聽聽聽聽聽 char(1)
聽 boolean聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 boolean聽聽聽聽聽聽聽聽聽聽聽聽聽 bit
聽 byte[]聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 binary聽聽聽聽聽聽聽聽聽聽聽聽聽聽 blob
聽 java.sql.Date聽聽聽聽聽聽聽聽聽聽 date聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Date
聽 java.sql.Timestamp聽聽聽聽聽 timestamp聽聽聽聽聽聽聽聽聽聽聽 Timestamp (杞芥暟鎹簱涓鏋滄彃鍏ヤ負null錛屾暟鎹簱緋葷粺鑷姩鎻掑叆涓哄綋鍓嶅?
2 琛ㄨ堪灞傦紞錛嶃嬩笟鍔¢昏緫灞傦紞銆媓ibernate錛嶃媎atabase
3
聽 Configuration config=new Configuration();
聽 config.add(Customer.class);
聽 sessionFactory=conf.buildSessionFactory();
聽 Session session=sessionFactory.openSession();
聽 Transaction tx;
聽 try{
聽聽 tx=session.beginTransaction();
聽聽 tx.commit();
聽 }catch(Exception e){
聽聽聽 if(tx!=null){
聽聽聽聽聽 tx.rollback();
聽聽聽 }
聽 }finally{
聽聽聽 session.close();
聽 }
4 鏁版嵁搴撳瓨鍙朾lob 瀵硅薄
聽1
聽聽 InputStream in=this.getClass().getResourceAsStream("photo.gif");
聽聽 byte[] buffer=new byte[in.available()]'
聽聽 in.read(buffer);
聽聽 customer.setImage(buffer);
聽2 byte[] buffer=customer.get.getImage();
聽聽 File OutputStream fout=new fileOutStream("photo.gif");
聽聽 fout.write(buffer);
聽聽 fout.close();

搴鋒枃 2006-08-18 16:20 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 欧美亚洲精品一区二区| 亚洲三级电影网站| 亚洲成av人片天堂网无码】| 国产人成免费视频网站| va天堂va亚洲va影视中文字幕| 777爽死你无码免费看一二区| 亚洲综合一区二区国产精品| 91免费国产自产地址入| 亚洲欧洲日产国码www| 国国内清清草原免费视频99| 亚洲欧洲日韩国产一区二区三区| 免费看韩国黄a片在线观看| 亚洲码和欧洲码一码二码三码| 免费无遮挡无码视频网站| 国产亚洲视频在线观看网址| 亚洲成a人片在线观看久| 精品国产福利尤物免费| 亚洲av无码一区二区三区不卡| 中文字幕在线免费观看| 亚洲а∨天堂久久精品9966| 国产一级高清视频免费看| 四虎精品免费永久免费视频| 婷婷精品国产亚洲AV麻豆不片 | 久久免费视频99| 亚洲妇女水蜜桃av网网站| 免费高清资源黄网站在线观看| 曰批免费视频播放在线看片二 | 亚洲日本中文字幕| AV片在线观看免费| 亚洲阿v天堂在线2017免费| 亚洲av不卡一区二区三区 | 一级做a爰全过程免费视频毛片| 亚洲小说区图片区另类春色| 久久免费的精品国产V∧| 亚洲风情亚Aⅴ在线发布| 在线观看亚洲av每日更新| 亚洲网站在线免费观看| 黄网站色成年片大免费高清| 婷婷久久久亚洲欧洲日产国码AV| 日本高清色本免费现在观看| 99视频在线看观免费|