{"id":199,"title":"SQL Notlar\u0131: Transactions","content":"<p>MySQL'de i\u015flemler (transactions), bir grup SQL ifadesini tek bir i\u015f birimi olarak y\u00f6netmeyi sa\u011flar. Bu, \u00f6zellikle veri tutarl\u0131l\u0131\u011f\u0131 ve b\u00fct\u00fcnl\u00fc\u011f\u00fc gerektiren durumlarda \u00f6nemlidir. Bu yaz\u0131da, MySQL'de i\u015flemlerin nas\u0131l ba\u015flat\u0131laca\u011f\u0131n\u0131, `COMMIT` ve `ROLLBACK` komutlar\u0131n\u0131n nas\u0131l kullan\u0131laca\u011f\u0131n\u0131 ve JDBC s\u00fcr\u00fcc\u00fcs\u00fc ile i\u015flemlerin nas\u0131l y\u00f6netilece\u011fini ele alaca\u011f\u0131z.<\/p><p>#### 1. \u0130\u015flem Ba\u015flatma (START TRANSACTION)<\/p><p>Bir i\u015flem, bir grup SQL ifadesini (SELECT, INSERT, UPDATE, DELETE) tek bir i\u015f birimi olarak y\u00f6netir. \u0130\u015flem i\u00e7indeki her bir i\u015flem ba\u015far\u0131l\u0131 olmal\u0131d\u0131r, aksi takdirde t\u00fcm i\u015flem geri al\u0131n\u0131r (ROLLBACK).<\/p><p>##### \u00d6rnek: \u0130\u015flem Ba\u015flatma ve COMMIT<\/p><p>A\u015fa\u011f\u0131daki \u00f6rnekte, iki hesap aras\u0131nda para transferi yap\u0131lan bir i\u015flem g\u00f6sterilmi\u015ftir:<\/p><p>```sql<br>START TRANSACTION;<\/p><p>SET @transAmt = 500;<br>SELECT @availableAmt := ledgerAmt FROM accTable WHERE customerId = 1 FOR UPDATE;<\/p><p>IF @availableAmt &gt;= @transAmt THEN<br>&nbsp; &nbsp;UPDATE accTable SET ledgerAmt = ledgerAmt - @transAmt WHERE customerId = 1;<br>&nbsp; &nbsp;UPDATE accTable SET ledgerAmt = ledgerAmt + @transAmt WHERE customerId = 2;<br>&nbsp; &nbsp;COMMIT;<br>ELSE<br>&nbsp; &nbsp;ROLLBACK;<br>END IF;<br>```<\/p><p>**ACID \u00d6zellikleri:**<\/p><p>- **Atomicity (Atomiklik):** \u0130\u015flem i\u00e7indeki t\u00fcm i\u015flemler ya tamamen ba\u015far\u0131l\u0131 olur ya da hi\u00e7biri uygulanmaz.<br>- **Consistency (Tutarl\u0131l\u0131k):** \u0130\u015flem ba\u015far\u0131l\u0131 bir \u015fekilde tamamland\u0131\u011f\u0131nda, veritaban\u0131 tutarl\u0131 bir durumda olur.<br>- **Isolation (\u0130zolasyon):** \u0130\u015flemler birbirinden ba\u011f\u0131ms\u0131z \u00e7al\u0131\u015f\u0131r.<br>- **Durability (Dayan\u0131kl\u0131l\u0131k):** \u0130\u015flem tamamland\u0131\u011f\u0131nda, sonu\u00e7lar kal\u0131c\u0131 olarak kaydedilir.<\/p><p>#### 2. COMMIT, ROLLBACK ve AUTOCOMMIT<\/p><p>##### AUTOCOMMIT<\/p><p>MySQL, varsay\u0131lan olarak `AUTOCOMMIT` modunu etkinle\u015ftirir. Bu, her SQL ifadesinin otomatik olarak commit edilmesi anlam\u0131na gelir. `AUTOCOMMIT` modunu devre d\u0131\u015f\u0131 b\u0131rakmak i\u00e7in:<\/p><p>```sql<br>SET AUTOCOMMIT = 0; &nbsp;-- AUTOCOMMIT'i devre d\u0131\u015f\u0131 b\u0131rak<br>SET AUTOCOMMIT = 1; &nbsp;-- AUTOCOMMIT'i etkinle\u015ftir<br>```<\/p><p>`AUTOCOMMIT` durumunu kontrol etmek i\u00e7in:<\/p><p>```sql<br>SELECT @@autocommit;<br>```<\/p><p>##### COMMIT<\/p><p>`AUTOCOMMIT` devre d\u0131\u015f\u0131 b\u0131rak\u0131ld\u0131\u011f\u0131nda, yap\u0131lan de\u011fi\u015fiklikler sadece mevcut ba\u011flant\u0131 i\u00e7in g\u00f6r\u00fcn\u00fcr olur. `COMMIT` komutu ile de\u011fi\u015fiklikler kal\u0131c\u0131 hale getirilir ve t\u00fcm ba\u011flant\u0131lar i\u00e7in g\u00f6r\u00fcn\u00fcr olur.<\/p><p>##### ROLLBACK<\/p><p>Bir hata olu\u015ftu\u011funda veya i\u015flem ba\u015far\u0131s\u0131z oldu\u011funda, `ROLLBACK` komutu ile yap\u0131lan t\u00fcm de\u011fi\u015fiklikler geri al\u0131n\u0131r.<\/p><p>##### \u00d6rnek: COMMIT ve ROLLBACK<\/p><p>```sql<br>-- AUTOCOMMIT'i devre d\u0131\u015f\u0131 b\u0131rak<br>SET AUTOCOMMIT = 0;<\/p><p>-- Yeni bir sat\u0131r ekle<br>INSERT INTO testTable VALUES (2), (3);<\/p><p>-- De\u011fi\u015fiklikleri commit et<br>COMMIT;<\/p><p>-- ROLLBACK \u00e7al\u0131\u015ft\u0131r (COMMIT'ten sonra ROLLBACK etkisizdir)<br>ROLLBACK;<br>```<\/p><p>#### 3. JDBC S\u00fcr\u00fcc\u00fcs\u00fc ile \u0130\u015flem Y\u00f6netimi<\/p><p>JDBC s\u00fcr\u00fcc\u00fcs\u00fc kullanarak MySQL'de i\u015flem y\u00f6netimi yap\u0131labilir. \u0130\u015flemler, `Connection` nesnesi \u00fczerinden y\u00f6netilir.<\/p><p>##### \u00d6rnek: JDBC ile \u0130\u015flem Y\u00f6netimi<\/p><p>```java<br>import java.sql.Connection;<br>import java.sql.DriverManager;<br>import java.sql.PreparedStatement;<br>import java.sql.ResultSet;<br>import java.sql.SQLException;<\/p><p>public class accTrans {<\/p><p>&nbsp; &nbsp;public static void doTransfer(double transAmount, int customerIdFrom, int customerIdTo) {<br>&nbsp; &nbsp; &nbsp; &nbsp;Connection con = null;<br>&nbsp; &nbsp; &nbsp; &nbsp;PreparedStatement pstmt = null;<br>&nbsp; &nbsp; &nbsp; &nbsp;ResultSet rs = null;<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp;try {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String DB_CONNECTION_URL = \"jdbc:mysql:\/\/localhost:3306\/testDB?useUnicode=true&amp;characterEncoding=utf8\";<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Class.forName(\"com.mysql.jdbc.Driver\");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con = DriverManager.getConnection(DB_CONNECTION_URL, \"user\", \"password\");<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ AUTOCOMMIT'i devre d\u0131\u015f\u0131 b\u0131rak<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con.setAutoCommit(false);<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Miktar kontrol\u00fc<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;double availableAmt = 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt = con.prepareStatement(\"SELECT ledgerAmt FROM accTable WHERE customerId = ? FOR UPDATE\");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.setInt(1, customerIdFrom);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rs = pstmt.executeQuery();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (rs.next()) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;availableAmt = rs.getDouble(1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (availableAmt &gt;= transAmount) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Miktar\u0131 \u00e7ek<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt = con.prepareStatement(\"UPDATE accTable SET ledgerAmt = ledgerAmt - ? WHERE customerId = ?\");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.setDouble(1, transAmount);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.setInt(2, customerIdFrom);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.executeUpdate();<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Miktar\u0131 yat\u0131r<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt = con.prepareStatement(\"UPDATE accTable SET ledgerAmt = ledgerAmt + ? WHERE customerId = ?\");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.setDouble(1, transAmount);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.setInt(2, customerIdTo);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pstmt.executeUpdate();<\/p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u0130\u015flemi commit et<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con.commit();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Yetersiz bakiye durumunda rollback<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con.rollback();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp;} catch (SQLException ex) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Hata durumunda rollback<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (con != null) con.rollback();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (SQLException e) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp;} finally {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Kaynaklar\u0131 serbest b\u0131rak<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (rs != null) rs.close();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (pstmt != null) pstmt.close();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (con != null) con.close();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} catch (SQLException ex) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ex.printStackTrace();<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp;}<\/p><p>&nbsp; &nbsp;public static void main(String args) {<br>&nbsp; &nbsp; &nbsp; &nbsp;doTransfer(500, 1020, 1021);<br>&nbsp; &nbsp;}<br>}<br>```<\/p><p>#### Sonu\u00e7<\/p><p>MySQL'de i\u015flemler, veri tutarl\u0131l\u0131\u011f\u0131 ve b\u00fct\u00fcnl\u00fc\u011f\u00fc sa\u011flamak i\u00e7in kritik \u00f6neme sahiptir. `START TRANSACTION`, `COMMIT` ve `ROLLBACK` komutlar\u0131 ile i\u015flemler y\u00f6netilebilir. JDBC s\u00fcr\u00fcc\u00fcs\u00fc kullanarak da Java uygulamalar\u0131nda i\u015flem y\u00f6netimi yap\u0131labilir. Bu y\u00f6ntemler, veritaban\u0131 i\u015flemlerini daha g\u00fcvenli ve tutarl\u0131 hale getirir. Bir sonraki yaz\u0131da g\u00f6r\u00fc\u015fmek \u00fczere!<\/p>","excerpt":"MySQL'de i\u015flemler (transactions), bir grup SQL ifadesini tek bir i\u015f birimi olarak y\u00f6netmeyi sa\u011flar. Bu, \u00f6zellikle veri tutarl\u0131l\u0131\u011f\u0131 ve b\u00fct\u00fcnl\u00fc\u011f\u00fc gerektiren durumlarda \u00f6nemlidir. Bu yaz\u0131da, MySQL'de i\u015flemlerin nas\u0131l ba\u015flat\u0131laca\u011f\u0131n\u0131, COMMIT ve ROLLBACK komutlar\u0131n\u0131n nas\u0131l kullan\u0131laca\u011f\u0131n\u0131 ve JDBC s\u00fcr\u00fcc\u00fcs\u00fc ile i\u015flemlerin nas\u0131l y\u00f6netilece\u011fini ele alaca\u011f\u0131z.","created_at":"2025-01-13 15:39:57","updated_at":"2026-09-08 03:21:32","category_id":9,"view_count":546,"reading_time":6,"status":"published","editor_choice":0,"is_editor_choice":0,"published_at":"2025-01-13 15:39:57","featured_image":"resimyok.jpg","slug":"sql-notlari-transactions","category_name":"Yaz\u0131l\u0131m","category_slug":"yazilim"}