Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. package ua.inf.smart.transaction;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class Transaction {
  10. public static void main(String[] args) throws SQLException {
  11. TransactionSingleton trAction = TransactionSingleton.getInstance();
  12. trAction.transfer("1000", 1, 1);
  13. }
  14. }
  15.  
  16. class TransactionSingleton {
  17. private Connection connFrom;
  18. private Connection connTo;
  19. private static TransactionSingleton instance = null;
  20. public synchronized static TransactionSingleton getInstance() {
  21. if (instance == null) {
  22. instance = new TransactionSingleton();
  23. instance.getConnTo();
  24. instance.getConnFrom();
  25. }
  26. return instance;
  27. }
  28. private Connection getConnFrom() {
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. connFrom = DriverManager.getConnection(
  32. "jdbc:mysql://localhost:3306/alfa_bank", "root", "1234");
  33. connFrom.setAutoCommit(false);
  34. } catch (SQLException e) {
  35. System.err.println("SQLException: " + e.getMessage()
  36. + "SQLState: " + e.getSQLState());
  37. } catch (ClassNotFoundException ex) {
  38. System.out.println("Driver not found");
  39. }
  40. return connFrom;
  41. }
  42. private Connection getConnTo() {
  43. try {
  44. Class.forName("com.mysql.jdbc.Driver");
  45. connTo = DriverManager.getConnection(
  46. "jdbc:mysql://127.0.0.1:3306/beta_bank", "root", "1234");
  47. connTo.setAutoCommit(false);
  48. } catch (SQLException e) {
  49. System.err.println("SQLException: " + e.getMessage()
  50. + "SQLState: " + e.getSQLState());
  51. } catch (ClassNotFoundException e) {
  52. System.err.println("Driver not found");
  53. }
  54. return connTo;
  55. }
  56. // method executes transfer
  57. public void transfer(String sumMove, int idFrom, int idTo) throws SQLException {
  58. Statement stmtFrom = null;
  59. Statement stmtTo = null;
  60. try {
  61. int sum = Integer.parseInt(sumMove);
  62. if (sum <= 0) {
  63. throw new NumberFormatException("less or equals zero");
  64. }
  65. stmtFrom = connFrom.createStatement();
  66. stmtTo = connTo.createStatement();
  67. // transaction of 4 queries
  68. ResultSet resSetFrom =
  69. stmtFrom.executeQuery("SELECT amount FROM account WHERE id=" + idFrom);
  70. ResultSet resSetTo =
  71. stmtTo.executeQuery("SELECT amount FROM account WHERE id=" + idTo);
  72. int amountFrom = 0;
  73. while (resSetFrom.next()) {
  74. amountFrom = resSetFrom.getInt(1);
  75. }
  76. int resultFrom = 0;
  77. if (amountFrom >= sum) {
  78. resultFrom = amountFrom - sum;
  79. } else {
  80. throw new SQLException("Invalid balance");
  81. }
  82. int amountTo = 0;
  83. while (resSetTo.next())
  84. amountTo = resSetTo.getInt(1);
  85. int resultTo = amountTo + sum;
  86.  
  87. stmtFrom.executeUpdate(
  88. "UPDATE account SET amount=" + resultFrom + " WHERE id=" + idFrom);
  89. stmtTo.executeUpdate(
  90. "UPDATE account SET amount=" + resultTo + " WHERE id =" + idTo);
  91. // transaction completion
  92. connFrom.commit();
  93. connTo.commit();
  94. System.out.println("Remaining on id_" + idFrom + ": " + resultFrom + " USD");
  95. System.out.println("Remaining on id_" + idTo + ": " + resultTo + " USD");
  96. } catch (SQLException e) {
  97. System.err.println("SQLState: " + e.getSQLState()
  98. + "Error Message: " + e.getMessage());
  99. // transaction rollback on error
  100. connFrom.rollback();
  101. connTo.rollback();
  102. } catch (NumberFormatException e) {
  103. System.err.println("Invalid summa: " + sumMove);
  104. } finally {
  105. if (stmtFrom != null) {
  106. try {
  107. stmtFrom.close();
  108. } catch (SQLException ex) {
  109. ex.printStackTrace();
  110. }
  111. }
  112. if (stmtTo != null) {
  113. try {
  114. stmtTo.close();
  115. } catch (SQLException ex) {
  116. ex.printStackTrace();
  117. }
  118. }
  119. if (connFrom != null) {
  120. try {
  121. connFrom.close();
  122. } catch (SQLException ex) {
  123. ex.printStackTrace();
  124. }
  125. }
  126. if (connTo != null) {
  127. try {
  128. connTo.close();
  129. } catch (SQLException ex) {
  130. ex.printStackTrace();
  131. }
  132. }
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement