Æ®·£Àè¼Ç
Æ®·£Àè¼Ç °ü¸®ÀÇ ¿¹´Â °èÁÂÀÌü°¡ °¡Àå ÁÁÀº ¿¹Á¦ÀÔ´Ï´Ù.
A °èÁ¿¡¼ B °èÁ·Π10000 ¿øÀ» ÀÌüÇÑ´Ù¸é
°úÁ¤ 1. A°èÁ¿¡¼ 10000¿øÀ» »«´Ù.
°úÁ¤ 2. B°èÁ¿¡¼ 10000¿øÀ» ´õÇÑ´Ù.
À§¿Í °°À» °Ì´Ï´Ù.
±×·±µ¥ ¸¸ÀÏ °úÁ¤ 1ÀÌ ¼º°øÇߴµ¥ °úÁ¤ 2°¡ ½ÇÆÐÇß´Ù¸é °úÁ¤ 1±îÁö Ãë¼ÒÇÏ°í °úÁ¤ 1ÀÌÀüÀÇ »óÅ·Πº¹±¸ÇÏ´Â °ÍÀÌ Æ®·£Àè¼Ç °ü¸®ÀÇ ¸ñÀûÀÔ´Ï´Ù.
Æ®·£Àè¼Ç °ü¸®¸¦ À§Çؼ °úÁ¤ 1°ú °úÁ¤ 2´Â ÇϳªÀÇ ´ÜÀ§·Î º¸´Â °ÍÀÌ °¡Àå Áß¿äÇÕ´Ï´Ù.
À̶§ °úÁ¤ 1°ú °úÁ¤ 2¸¦ ¹¾î¼ Æ®·£Àè¼ÇÀÇ ´ÜÀ§¶ó°í ÇÕ´Ï´Ù.
JDBC ÇÁ·Î±×·¥¿¡¼ ¾òÀº Ä¿³Ø¼ÇÀº ±âº»ÀûÀ¸·Î auto commit ¸ðµåÀÔ´Ï´Ù.
auto commit ¸ðµå´Â SQL ¹®Àå Çϳª Çϳª¸¦ Æ®·£Àè¼Ç ´ÜÀ§·Î º¸´Â °ÍÀÔ´Ï´Ù.
À̸¦ ÇØÁ¦Çϱâ À§Çؼ´Â auto commit ¸ðµå¸¦ false ·Î ¼³Á¤Çϰí ÇÁ·Î±×·¥»ó¿¡¼ commit½ÃÁ¡À» Á¤ÀÇÇÏ´Â °ÍÀÌ Æ®·¢Àè¼Ç °ü¸® ÄÚµù¹æ¹ýÀÔ´Ï´Ù.
°èÁÂÀÌü¿¡ Àû¿ëÇϸé,
con.setAutoCommit( false );
°úÁ¤ 1. A°èÁ¿¡¼ 10000¿øÀ» »«´Ù.
°úÁ¤ 2. B°èÁ¿¡¼ 10000¿øÀ» ´õÇÑ´Ù.
con.commit();
ÀÌÁ¦ ¿ì¸®ÀÇ Ä¿ÇÇ™ö ¿¹Á¦·Î µ¹¾Æ¿Í¼ auto commit ¸ðµå¸¦ false ·Î ¼³Á¤Çϰí ÇÁ·Î±×·¥»ó¿¡¼ commit ½ÃÁ¡À» Á¤ÀÇÇÏ´Â ¿¹Á¦¸¦ ±¸ÇöÇÕ´Ï´Ù.
TransactionPairs.javaÀ» JDBC ÇÁ·Î±×·¥ ¼ø¼·Î ÄÚµùÇÕ´Ï´Ù.
- JDBC µå¶óÀ̹ö ·Îµù
- Connection ¸Î±â
- SQL ½ÇÇà
- [SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ Ã³¸®]
- ÀÚ¿ø ¹Ýȯ
TransactionPairs.java
import java.sql.*;
public class TransactionPairs {
public static void main( String args[] ) {
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Connection con = null;
Statement stmt;
PreparedStatement updateSales;
PreparedStatement updateTotal;
String updateString = "UPDATE coffees " +
"SET sales = ? WHERE cof_name LIKE ?";
String updateStatement = "UPDATE coffees " +
"SET total = total + ? WHERE cof_name LIKE ?";
String query = "SELECT cof_name, sales, total FROM coffees";
try {
Class.forName( "oracle.jdbc.driver.OracleDriver" );
} catch( java.lang.ClassNotFoundException e ) {
System.err.print( "ClassNotFoundException: " );
System.err.println( e.getMessage() );
}
try {
con = DriverManager.getConnection( url, "scott", "tiger" );
updateSales = con.prepareStatement( updateString );
updateTotal = con.prepareStatement( updateStatement );
int [] sales = {175, 150, 60, 155, 90};
int [] totals = {1350, 1230, 2160, 2355, 3290};
String [] coffees = {"Colombian", "French_Roast",
"Espresso", "Colombian_Decaf",
"French_Roast_Decaf"};
int len = coffees.length;
con.setAutoCommit( false );
for ( int i = 0; i < len; i++ ) {
updateSales.setInt( 1, sales[i] );
updateSales.setString( 2, coffees[i] );
updateSales.executeUpdate();
updateTotal.setInt( 1, sales[i] );
updateTotal.setString( 2, coffees[i] );
updateTotal.executeUpdate();
con.commit();
}
for ( int i = 0; i < len; i++ ) {
updateTotal.setInt( 1, totals[i] );
updateTotal.setString( 2, coffees[i] );
updateTotal.executeUpdate();
updateTotal.setInt( 1, totals[i] );
updateTotal.setString( 2, coffees[i] );
updateTotal.executeUpdate();
con.commit();
}
con.setAutoCommit( true );
updateSales.close();
updateTotal.close();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( query );
while ( rs.next() ) {
String c = rs.getString( "cof_name" );
int s = rs.getInt( "sales" );
int t = rs.getInt( "total" );
System.out.println( c + " " + s + " " + t );
}
stmt.close();
con.close();
} catch( SQLException ex ) {
System.err.println( "SQLException: " + ex.getMessage() );
if ( con != null ) {
try {
System.err.print( "Transaction is being ");
System.err.println( "rolled back" );
con.rollback();
} catch( SQLException excep ) {
System.err.print( "SQLException: " );
System.err.println( excep.getMessage() );
}
}
}
}
}
TransactionPairs.java ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.
total ÀÇ °æ¿ì ´©ÀûµÇ¹Ç·Î °ªÀº ¾Æ·¡¿Í ´Ù¸¦ ¼ö ÀÖ½À´Ï´Ù.
D:\>javac TransactionPairs.java D:\>java TransactionPairs Espresso 60 8820 Colombian_Decaf 155 9885 French_Roast_Decaf 90 13430 Colombian 175 5925 French_Roast 150 5370 D:\>
- ´ÙÀ½ °Á : Connection Pool
- ÀÌÀü °Á : Å×À̺í Á¶ÀÎ(Join)
