¡¡¡¡ÆóÒµÊý¾Ý¿âÁ¬½ÓÐÔµÄÇ¡µ±¹¹½¨ÊǷdz£ÖØÒªµÄ£¬²¢ÇÒÔÚΪÓÐÏÞÁ¬½ÓÉ豸ÅäÖã¨Connection Limited Device Configuration£¨CLDC£©£©¹¹½¨Ó¦ÓóÌÐòºÍ±àдÌض¨ÓÚ MIDP£¨Òƶ¯½Ó¿Ú£©µÄ½Ó¿ÚÖ®¼ä£¬ÄÄÒ»ÖÖÊÇ×î¼ÑÑ¡ÔñÈ¡¾öÓÚ J2ME ºÍ Java ¼¼Êõ¡£
¡¡¡¡·½Ê½
¡¡¡¡½éÉÜÔÚ°ÑÆóÒµÊý¾ÝÇŽӵ½ÎÞÏßÒƶ¯É豸ʱ½¨Á¢ JDBC Á¬½ÓµÄÒ»Ìõ¼òµ¥ÔÔò£¬Õâ¿ÉÒÔ°ïÖúÄúʹ½â¾ö·½°¸¾ßÓиüºÃµÄ¿ÉÀ©Õ¹ÐԺ͸ü¸ßЧµÄÐÔÄÜ¡£
¡¡¡¡Á¬½Ó³Ø
¡¡¡¡ÈκÎÓ¦ÓóÌÐò¶¼±ØÐëÏÈ·ÃÎʻµÄÊý¾Ý¿âÁ¬½Ó£¬È»ºó²ÅÄÜ·ÃÎÊÊý¾Ý¿â¡£Êý¾Ý¿âÁ¬½ÓÊÇÒ»¸öºÜÕ¼×ÊÔ´ÇÒ I/O ¿ªÏúºÜ´óµÄ²Ù×÷£¬²¢ÇÒÈç¹ûÿ´ÎÏëʹÓÃÊý¾Ý¿âÁ¬½Óʱ¶¼±ØÐë´´½¨Ëü£¬ÄÇôËü½«»á³ÉΪÄúµÄÐÔÄÜÆ¿¾±¡£
¡¡¡¡ÀýÈ磬Èç¹ûÄúʹÓà Java servlet£¨Java servlet ͨ¹ý init() ·½·¨´´½¨²¢ÔÚÆäÉúÃüÖÜÆÚ½áÊøʱ±»Ïú»Ù£¨Í¨¹ý destroy() ·½·¨£©£©µÄ·½Ê½£¬ÄúÊÇËäÈ»±ÜÃâÁËÿ´Î servlet ±»ÊµÀý»¯Ê±ÖØн¨Á¢Á¬½Ó¡£ÕâÑùÒ»ÖÖ·½Ê½»áÃ÷ÏԵؽµµÍÓ¦ÓóÌÐòµÄÐÔÄÜ¡£Íê³ÉÏàͬ¹¦ÄܵĸüºÃµÄ;¾¶ÊÇʹÓá°Á¬½Ó³Ø£¨Connection Pool£©¡±£¬Äú¿ÉÒÔÔÚÁ¬½Ó³ØÖгõʼ»¯¶à¸öÁ¬½Ó£¨²¢ÇÒ²ÎÊý¿ÉÒÔ´Ó XML ÅäÖÃÎļþÖжÁÈ¡£©¡£
¡¡¡¡Á¬½Ó±¾ÉíÓÉÒ»×鼯ºÏ¶ÔÏóºÍÒ»¸öÔÚÕû¸öÇëÇó¹ý³ÌÖÐʹÁ¬½Ó±£³Ö´ò¿ªµÄÓû§ÇëÇó×é³É¡£´´½¨Á¬½Ó³ØµÄ¹Ø¼üÊÇÔÚÊý¾Ý¿â·ÃÎÊ´úÂëÖÐʹÓÃÈçÏÂһЩ¿é£ºtry{}.. catch{}... finally{}..¡£È»ºóÄúʹÓà close() ·½·¨À´È·ÈÏÁ¬½Óȷʵ±»·µ»Øµ½ÁËÁ¬½Ó³Ø¶ø²»ÊDZ»³¹µ×¹Ø±ÕÁË¡£ÔÚ¡°finally{}¡±¿éÖÐÖ¸¶¨ close() ·½·¨Ê¹µÃÖ´Ðйý³ÌÖз¢ÉúµÄÒì³£»á±»²¶»ñµ½£¬²¢ÇÒ¸ÃÓï¾äÈÔ±»Ö´ÐÐ -- Á¬½Ó·µ»Øµ½Á¬½Ó³Ø£¬Õâ¾Í·ÀÖ¹ÁËÓ¦ÓóÌÐòÖС°Á¬½Óй©£¨connection leak£©¡±µÄ·¢Éú¡£
¡¡¡¡ÒÔÏÂÊǹ¹½¨Ò»¸ö JDBC Á¬½ÓµÄʾÀý£º
Connection con = null;
try { ds = (DataSource)myContext.lookup("");
pooledCon = ds.getConnection("scott", "tiger");
// Processing Code goes here
} catch (Exception ignored) {
// catch JNDI or JDBC exceptions here
} finally {
if(pooledCon != null)
pooledCon.close();
}
¡¡¡¡Ê¹Óà PreparedStatement
¡¡¡¡ÈËÃÇÈÏΪ PreparedStatement ¶ÔÏóµÄЧÂʱȶà¸ö Statement ¶ÔÏó¸ü¸ß£¬ÓÈÆäÊÇÈç¹ûÄú±ØÐë¶à´ÎÖ´ÐÐͬһÌõÓï¾ä¶ø²î±ð½öÔÚÓÚ²ÎÊý²»Í¬Ê±¸üÊÇÈç´Ë¡£PreparedStatement ÔÊÐíÄú½« SQL Óï¾ä¡°±àÒ롱һ´Î£¨¾¡¹ÜÕâÖÖ±àÒëµÚÒ»´ÎÒªÏûºÄ½Ï¶àµÄʱ¼ä£©£¬È»ºó½«Ëü±£´æÔÚ¸ßËÙ»º´æÖУ¬´Ó¶øʵÏÖÓÐЧµÄÖØÓá£Í¬Ê±ËüÒ²ÌṩÁ˿ɶÁÐÔ¸üºÃµÄ´úÂë¡£
¡¡¡¡ÁíÒ»¸ö¶îÍâµÄÓÅÊÆÊÇÓÉÇý¶¯³ÌÐòÍê³ÉµÄ¶ÔÓû§´«µÝ¸øÓï¾äµÄ×Ö·û´®µÄ×Ô¶¯×ªÒå¡£¾ÙÀýÀ´Ëµ£¬ÕâÒâζ×ŵ±ÄúÊÔͼ½«×Ö·û´®¡°D'Marco¡±²åÈëµ½Ò»¸ö»ùÓÚ×Ö·ûµÄÊý¾ÝÓò£¨Ëü¿ÉÄÜÊÇ VARCHAR, VARCHAR2, CHAR µÈ£©ÖÐʱ£¬SQL Óï¾ä²»»áÔÚÓöµ½µÚÒ»¸öƲºÅʱ¾Í²úÉúÔÖÄÑÐÔµÄʧ°Ü¡£
¡¡¡¡Ê¹Óà PreparedStatement ¶ÔÏóʱµÄÁíÒ»¸öÁ¼ºÃÏ°¹ßÊǵ÷ÓöÔÏó×ÔÉíµÄ close() ·½·¨À´¡°¹Ø±Õ¶ÔÏó¡±£¬Õâ¸ö·½·¨½«±»ÓÃÀ´ÔËÐÐ SQL Óï¾ä¡£Õâ»á¹Ø±ÕÈκÎÓëÕýÔÚÖ´ÐÐµÄ SQL Óï¾äÏà¹ØÁªµÄÓα꣬ÕâÑù¾ÍÄÜ·ÀÖ¹´ò¿ªµÄÓαê°ÑÊý¾Ý¿âŪµÃÊ®·ÖÁèÂÒ¡£
¡¡¡¡ÒÔÏÂÊÇÒ»¸ö´´½¨ PreparedSatement µÄʾÀý£º
PreparedStatement sqlstmt = dbCon.prepareStatement("select *
from table1 where field_1=?");
sqlstmt.setInt(1, 12);
ResultSet rs = sqlstmt.executeQuery();
// close the resultset statement to avoid hanging cursors in database
sqlstmt.close();
// processing of new statement
sqlstmt = dbCon.prepareStatement("select * from table2 where field_2 = ?");
// repeat creating the result set
¡¡¡¡Ç¡µ±µØÀûÓÃÊÂÎñ
¡¡¡¡ÔÚ¸üж¯Ì¬Êý¾Ý¿â±íºÍÊý¾Ýʱ³£³£»áÒÅÍüµÄÒ»¸ö·½Ã棬¾ÍÊÇÔÚÏò³¬¹ýÒ»¸ö±íʾһ¸öÂß¼ÊÂÎñµÄ±í¸üлò²åÈëÊý¾Ýʱ£¬Õâ¸öÊÂÎñÓ¦¸Ã·´Ó³µ½ËùÓеıíÖУ¬»òÕßÔÚÅöµ½ÊÂÎñʧ°Üʱ£¬Í¨¹ý¡°»Ø¹ö¡±¸ÃÊÂÎñ¶øÔÚÿһ¸ö±íÖж¼Ã»Óз´Ó³¡£
¡¡¡¡Ò»Ð©ºËÐÄµÄ JDBC °üÖ§³ÖÁËËÄÖÖÊÂÎñ¸ôÀëģʽ£¨transaction isolation mode£©£¬ÕâЩģʽÔÊÐí³ÌÐòÖ¸¶¨ËüÃÇÏëÊÂÎñ±íÏÖ³öÔõÑùµÄÐÐΪ¡£´ó¶àÊý³ÌÐò¶¼ÖÁÉÙÖ§³ÖÁ½ÖÖģʽ£º¡°¶ÁÈ¡Ìá½»£¨read committed£©¡±£¨È±Ê¡Öµ£©ºÍ¡°¿ÉÐòÁл¯µÄ£¨serializable£©¡±¡£µ±²»¿ÉÖظ´¶ÁÈ¡Ó¦¸ÃÔÊÐíÔÚ¶à¸ö²éѯ֮¼äÓÉÒ»¸öÊÂÎñ×÷³öµÄÐ޸ĶÔÓÚÁíÒ»¸öÊÂÎñ¿É¼ûʱ£¬ÇëʹÓá°¶ÁÈ¡Ìá½»¡±£»ÒªÊ¹ÓÉÁíÒ»¸öÊÂÎñ×÷³öµÄÐÞ¸ÄÔÚÒ»¸ö²éѯÔËÐÐʱ³ÉΪ¿É¼ûµÄ£¬ÇëʹÓûÃÏó¶ÁÈ¡£¨phantom read£©¡£µ±ÄúÐèÒªÒ»¸ö¿ç¶à¸ö²Ù×÷Ç°ºóÍêÈ«Ò»ÖµÄÊý¾Ý¿âÊÓͼʱ£¬¾ÍÓ¦¸ÃʹÓøüΪÑϸñµÄ¡°¿ÉÐòÁл¯µÄ¡±ÉèÖᣰÑÁ¬½ÓµÄ×Ô¶¯Ìá½»ÉèÖÃΪ¡°¼Ù¡±£¨autocommit = "false"£©£¬¼ÇסÕâÒ»µãÊǺÜÓÐÓõġ£
¡¡¡¡ÒÔÏÂÊǹ¹½¨Ò»¸öÁ¬½Ó²¢ÉèÖÃÆä¸÷¸öÊôÐÔ²ÎÊýµÄʾÀý£º
Connection con = null;
try {
dtsr = (DataSource ");
pConn = dtsr.getConnection("", "");
pConn.setAutoCommit(false); // transaction are not committed uponm execution
pConn.setTransactionIsolation(
Connection.TRANSACTION_SERIALIZABLE);
// pConn is pooled connection
pConn.commit();
} catch (Exception ignored) {
try { pConnn.rollback(); } catch (SQLException esgl) {}
} finally {
if(pConn != null) {
pConn.setAutoCommit(true); //reset autocommit
pConn.close();
}
}
¡¡¡¡ÄúÒ²¿ÉÒÔÀûÓÃÒ»¸ö¿ÉÑ¡µÄ JDBC °ü -- JTA£¨Java Transaction API£©£¬ËüÔÊÐíÈÝÒ׵غÍÍêÈ«¶ÀÁ¢µÄÊÂÎñ·þÎñÆ÷½øÐм¯³É¡£