วันนี้สร้างตารางขึ้นมาชื่อว่า booking โดยเป็น weak entity แบบ M:N ทีนี้สร้างในฐานข้อมูลจริงไม่ได้ เกิด error 121 เลยไปหาในเน็ตทางแก้คือเปลี่ยนชื่อ attribute ของ table booking ที่ fk ไป table อื่นซะ ลองดูในเว็บที่ฝรั่งถามไว้นะครับ
http://stackoverflow.com/questions/3316410/sql-error-errno-121
Tuesday, April 9, 2013
Thursday, March 28, 2013
การใช้ Transactions ใน Java
Disabling Auto-Commit Mode
เมื่อทำการสร้าง connection นั้น จะอยู่ในโหมด Auto Commit โดยอัตโนมัติ นั่นหมายถึงว่าแต่ละคำสั่ง SQL ที่กระทำ transaction นั้นจะ Commit(ยืนยัน) หมด(ถ้าจะให้ละเอียดคือ ในคำสั่ง SQL นั้นจะ Commit เมื่อมันเสร็จสิ้นแล้ว ไม่ใช่ในขณะทำงาน(excuted) )
ในการ disable commit คือ con.setAutoCommit(false);
หลังจากที่เราได้ทำการ disable commit ไปแล้วจะไม่มีคำสั่ง SQL Statement ไหน commit จนกว่าเราจะใช้คำสั่ง commit เอง คำสั่ง SQL ทั้งหมดจะถูก excute หลังจากเรียกคำสั่ง commit คือเพิ่มเข้าไปใน transection ปัจจุบันและ commit รวมกันเป็นกลุ่ม ดังตัวอย่าง
public void updateCoffeeSales(HashMapsalesForWeek) throws SQLException { PreparedStatement updateSales = null; PreparedStatement updateTotal = null; String updateString = "update " + dbName + ".COFFEES " + "set SALES = ? where COF_NAME = ?"; String updateStatement = "update " + dbName + ".COFFEES " + "set TOTAL = TOTAL + ? " + "where COF_NAME = ?"; try { con.setAutoCommit(false); updateSales = con.prepareStatement(updateString); updateTotal = con.prepareStatement(updateStatement); for (Map.Entry e : salesForWeek.entrySet()) { updateSales.setInt(1, e.getValue().intValue()); updateSales.setString(2, e.getKey()); updateSales.executeUpdate(); updateTotal.setInt(1, e.getValue().intValue()); updateTotal.setString(2, e.getKey()); updateTotal.executeUpdate(); con.commit(); } } catch (SQLException e ) { JDBCTutorialUtilities.printSQLException(e); if (con != null) { try { System.err.print("Transaction is being rolled back"); con.rollback(); } catch(SQLException excep) { JDBCTutorialUtilities.printSQLException(excep); } } } finally { if (updateSales != null) { updateSales.close(); } if (updateTotal != null) { updateTotal.close(); } con.setAutoCommit(true); } }
ใน method นี้จะ auto-commit mode เป็น disable ซึ่งหมายถึง 2 prepared statements จะ commit รวมกัน เมื่อ commit มีการเรียก
ในคำสั่ง con.setAutoCommit(true); จะหมายถึงการกลับไปยัง สถานะ default เมื่อคำสั่งต่างๆเสร็จสิ้้น
Using Transactions to Preserve Data Integrity
ตัวอย่างของ transaction isolation level คือTRANSACTION_READ_COMMITTED จะไม่ยอมรับการเข้าถึงข้อมูลจนกว่า transaction เสร็จสิ้น พูดอย่างอย่างคือ DBMS จะไม่ยอมรับการมี dirty reads เกิดขึ้นTransaction Isolation Level แบ่งออกเป็น 5 ระดับ ดังนี้
Isolation Level Transactions Dirty Reads Non-Repeatable Reads Phantom Reads TRANSACTION_NONENot supported Not applicable Not applicable Not applicable TRANSACTION_READ_COMMITTEDSupported Prevented Allowed Allowed TRANSACTION_READ_UNCOMMITTEDSupported Allowed Allowed Allowed TRANSACTION_REPEATABLE_READSupported Prevented Prevented Allowed TRANSACTION_SERIALIZABLESupported Prevented Prevented Prevented non-repeatable read เกิดขึ้นเมื่อ transaction A รับค่าแถว,ลำดับต่อมา transaction B update แถว และลำดับต่อมา transaction A รับค่าที่แถวเดิมอีกครั้ง transaction A รับค่าที่แถวเดิม 2 ครั้งแต่ได้ค่าต่างไปจากเดิมphantom read เกิดขึ้นเมื่อ transaction A รับค่าจะแถวโดยผ่าน condition (where),ลำดับต่อมา transaction B ก็ทำการเพิ่มและ update แถวโดยผ่าน condition เดียวกันกับ transaction A ต่อมา transaction A ก็มารับค่าตาม condition อีกครั้ง แต่ปรากฏว่าเห็นแถวเพิ่มขึ้น แถวที่เพิ่มขึ้นนี้เองเรียกว่า phantomโดยทั่วไปเราไม่ต้องทำอะไรเกี่ยวกับTransaction Isolation Level สามารถใช้ค่า defalut ของ DBMS ได้เลย ค่า default ของ DBMS จะขึ้นอยู่กับ DBMS ที่ใช้ เช่น Java DB คือTRANSACTION_READ_COMMITTED ถ้าต้องการหาว่า DBMS ใช้ Level ไหนใช้คำสั่งConnectionmethodgetTransactionIsolation และถ้าต้องการ set เองใช้ConnectionmethodsetTransactionIsolationNote JDBC driver อาจจะไม่ support transaction level ทั้งหมด ถ้าไม่ support level ที่ระบุในsetTransactionIsolation ก็สามารถสลับไปใช้ level ที่สูงกว่า ถ้า driver ไม่สามารถสลับไปที่ level ที่สูงกว่าได้จะเกิด SQLException ใช้ methodDatabaseMetaData.supportsTransactionIsolationLevel ในการหาว่า driver support level ไหนSetting and Rolling Back to Savepoints
methodConnection.setSavepoint คือ set Savepoint Object ใน transaction ปัจจุบันConnection.rollbackmethod คือส่ง Savepoint ไปเป็น agumentตัวอย่างถ้าต้องการเพิ่มราคาแล้วราคานั้นเกินกว่าที่กำหนดไว้ก็จะ rollback ไปค่าเริ่มต้นใหม่อีกครั้งpublic void modifyPricesByPercentage( String coffeeName, float priceModifier, float maximumPrice) throws SQLException { con.setAutoCommit(false); Statement getPrice = null; Statement updatePrice = null; ResultSet rs = null; String query = "SELECT COF_NAME, PRICE FROM COFFEES " + "WHERE COF_NAME = '" + coffeeName + "'"; try { Savepoint save1 = con.setSavepoint(); getPrice = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); updatePrice = con.createStatement(); if (!getPrice.execute(query)) { System.out.println( "Could not find entry " + "for coffee named " + coffeeName); } else { rs = getPrice.getResultSet(); rs.first(); float oldPrice = rs.getFloat("PRICE"); float newPrice = oldPrice + (oldPrice * priceModifier); System.out.println( "Old price of " + coffeeName + " is " + oldPrice); System.out.println( "New price of " + coffeeName + " is " + newPrice); System.out.println( "Performing update..."); updatePrice.executeUpdate( "UPDATE COFFEES SET PRICE = " + newPrice + " WHERE COF_NAME = '" + coffeeName + "'"); System.out.println( "\nCOFFEES table after " + "update:"); CoffeesTable.viewTable(con); if (newPrice > maximumPrice) { System.out.println( "\nThe new price, " + newPrice + ", is greater than the " + "maximum price, " + maximumPrice + ". Rolling back the " + "transaction..."); con.rollback(save1); System.out.println( "\nCOFFEES table " + "after rollback:"); CoffeesTable.viewTable(con); } con.commit(); } } catch (SQLException e) { JDBCTutorialUtilities.printSQLException(e); } finally { if (getPrice != null) { getPrice.close(); } if (updatePrice != null) { updatePrice.close(); } con.setAutoCommit(true); } }method เริ่มต้นโดยสร้าง Savepoint ด้วยคำสั่ง Savepoint save1 = con.setSavepoint();method จะทำการตรวจสอบค่า new price ว่ามากกว่า maximum price หรือไม่ถ้ามากกว่าก็ให้กลับไปค่าปัจจุบัน con.rollback(save1);ดังนั้น เมื่อ method commit transaction โดยเรียกConnection.commitmethod มันจะไม่ commit แถวใดๆที่มีความสัมพันธ์กับ Savepoint ที่มีการ rolled back มันจะ commit แถวอื่นReleasing Savepoints
methodConnection.releaseSavepoint โดยป้อน Savepoint Object เป็น parameter และจะ remove Savepoint จาก transaction ปัจจุบันSavepoint จะ Release โดยอัตโนมัติ เมื่อมีการ commit หรือเมื่อ transaction rolle backการเรียน method rollback จะทำให้ออกจาก transaction และทำการ return ค่าใดที่ทำการเปลียนแปลงก่อนหน้าให้เป็นค่าเริ่มต้น ถ้าคุณทดลอง execute statment ใน transaction จะทำให้เกิด SQLException การเรียก method จะทำให้ transaction จบ และทำการเริ่มต้น transacton ใหม่อีกครั้ง
Saturday, December 1, 2012
deep freeze v.7 ไม่เห็นใน Task bar
ผมได้ทำการแช่แข็ง Harddisk ไว้ แล้วทีนี้จะทำการแก้ไข window นิดนึงจึงทำการปลดล็อคแต่ปรากฎว่า
ปลดล็อกไม่ได้ เนื่องจาก ไม่เห็นรูปไอคอน deep freeze "มันหายไปไหนเนี่ย" หาอยู่ตั้งนานว่าจะทำไง เกือบลง window ใหม่แล้วเชียว ไปพบวิธีแก้ซะก่อน คือ
-ให้ทำการ logout ก่อนนะ
-จากนั้นก็เข้า window ใหม่ ไม่ต้อง boot เครื่องใหม่นะกดเข้าไปเลย
ก็จะเห็น deep freeze และก็จัดการได้เลย ง่ายซะไม่มี
ปลดล็อกไม่ได้ เนื่องจาก ไม่เห็นรูปไอคอน deep freeze "มันหายไปไหนเนี่ย" หาอยู่ตั้งนานว่าจะทำไง เกือบลง window ใหม่แล้วเชียว ไปพบวิธีแก้ซะก่อน คือ
-ให้ทำการ logout ก่อนนะ
-จากนั้นก็เข้า window ใหม่ ไม่ต้อง boot เครื่องใหม่นะกดเข้าไปเลย
ก็จะเห็น deep freeze และก็จัดการได้เลย ง่ายซะไม่มี
Sunday, April 15, 2012
JavaScript undefined Properties
เป็นการระบุว่า ไม่มีการกำหนดค่าให้กับตัวแปร ดังตัวอย่าง
ได้ผลลัพธ์คือ
t2 is undefined
จะเห็นได้ว่า ตัวแปร t2 ยังไม่ได้กำหนดค่าให้ทำให้มีคุณสมบัติเป็น undefined
ได้ผลลัพธ์คือ
t2 is undefined
จะเห็นได้ว่า ตัวแปร t2 ยังไม่ได้กำหนดค่าให้ทำให้มีคุณสมบัติเป็น undefined
Saturday, April 7, 2012
Sunday, March 25, 2012
Callback Function ใน DWR
หลักการที่สำคัญใน Ajax คือ callback function หรือพูดง่ายคือ ฟังก์ชั่นที่จะรับข้อมูลกลับเพื่อทำงานต่อนั่นเอง ใน DWR callback ใช้ทำงานกับข้อมูลที่ส่งมาจาก method
ประเภทของ callback function คือ 1.
1.ถ้าฟังก์ชั่นไม่มี parameter เช่น HorizontalMenu.getMenuItems(setMenuItems) ฟังก์ชั่นนี้จริงๆแล้วไม่มี parameter แต่ในการเรียกใช้โดย javascript เราจึงได้เพิ่ม callback function เข้าไป คือ setMenuItems นั่นเอง 2.
2.ถ้าฟังก์ชั่นนั้นมี parameter เช่น CountryDB.getContries(selectLetters,setCountryRows) โดย selectLetters คือ input parameter และ setCountryRows คือ callback function
สำหรับแบบนี้ ในการใช้ callback function คือ meta-data object ในการที่ javascript จะเรียกใช้งาน เช่น CountryDB.saveCountryNotes(ccode,newNotes,{ callback:function(newNote)
{ //function body here
} });
ฟังก์ชั่นนี้ คือ anonymous และมัน implementation โดยการเพิ่ม javascript ที่เรียก เข้าไปยัง remote Java method ประโยชน์ในการทำลักษณะนี้คือ ง่ายในการอ่าน และจะ execute ทันทีหลังจากได้รับค่าที่ return มาจาก Java method
สำหรับประโยชน์อื่นๆคือเราสามารถเพิ่ม option ในการเรียกได้ คือสามารถเพิ่ม timeout และ error handler เช่น
CountryDB.saveCountryNotes(ccode,newNotes,{ callback:function(newNotes)
{ //function body here }, timeout:10000, errorHandler:function(errorMsg){ alert(errorMsg);} });
มีความเป็นไปได้ว่า Java method จะไม่ return ค่ากลับมา ในการเพิ่ม callback ไปยัง method ที่ไม่ return ค่า จะมีประโยชน์ในการรับการแจ้งเตือน เมื่อ remote call ทำงานเสร็จ
ประเภทของ callback function คือ 1.
1.ถ้าฟังก์ชั่นไม่มี parameter เช่น HorizontalMenu.getMenuItems(setMenuItems) ฟังก์ชั่นนี้จริงๆแล้วไม่มี parameter แต่ในการเรียกใช้โดย javascript เราจึงได้เพิ่ม callback function เข้าไป คือ setMenuItems นั่นเอง 2.
2.ถ้าฟังก์ชั่นนั้นมี parameter เช่น CountryDB.getContries(selectLetters,setCountryRows) โดย selectLetters คือ input parameter และ setCountryRows คือ callback function
สำหรับแบบนี้ ในการใช้ callback function คือ meta-data object ในการที่ javascript จะเรียกใช้งาน เช่น CountryDB.saveCountryNotes(ccode,newNotes,{ callback:function(newNote)
{ //function body here
} });
ฟังก์ชั่นนี้ คือ anonymous และมัน implementation โดยการเพิ่ม javascript ที่เรียก เข้าไปยัง remote Java method ประโยชน์ในการทำลักษณะนี้คือ ง่ายในการอ่าน และจะ execute ทันทีหลังจากได้รับค่าที่ return มาจาก Java method
สำหรับประโยชน์อื่นๆคือเราสามารถเพิ่ม option ในการเรียกได้ คือสามารถเพิ่ม timeout และ error handler เช่น
CountryDB.saveCountryNotes(ccode,newNotes,{ callback:function(newNotes)
{ //function body here }, timeout:10000, errorHandler:function(errorMsg){ alert(errorMsg);} });
มีความเป็นไปได้ว่า Java method จะไม่ return ค่ากลับมา ในการเพิ่ม callback ไปยัง method ที่ไม่ return ค่า จะมีประโยชน์ในการรับการแจ้งเตือน เมื่อ remote call ทำงานเสร็จ
Subscribe to:
Posts (Atom)