Guest User

Untitled

a guest
Jul 26th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. Reducing commits and inserting bulk records at a time in jdbc call?
  2. public final class RequestFilter implements Filter{
  3.  
  4. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
  5. try {
  6. ((HttpServletResponse) response).sendRedirect(redirectUrl);
  7.  
  8. if (chain != null) {
  9. chain.doFilter(request, response);
  10. }
  11. }
  12. finally { // commit transaction here }
  13. }
  14. }
  15.  
  16. DBLog.logService(param1, param2); // static method call
  17.  
  18. public static void logService(String param1, String param2) {
  19.  
  20. try {
  21.  
  22. con=getConnection();
  23. stmt = con.prepareCall("{call DB_LOG_SP (?, ?}");
  24. stmt.setString(1, param1);
  25. stmt.setString(2, param2);
  26. stmt.execute();
  27. stmt.close();
  28. }finally {
  29. stmt.close();
  30. con.close();
  31. }
  32. }
  33.  
  34. public class DBLog {
  35.  
  36. static Connection con = null;
  37. static PreparedStatement stmt = null;
  38.  
  39. static {
  40. try{
  41. new net.verizon.whatsnext.dblog.JDCConnectionDriver("oracle.jdbc.driver.OracleDriver",
  42. "jdbc:oracle:thin:@localhost:7001:xe","user", "password");
  43. con=getConnection(); // Getting connection from Connection pool
  44.  
  45. }catch(Exception e){}
  46. }
  47.  
  48. public static Connection getConnection() throws SQLException {
  49. return DriverManager.getConnection("jdbc:jdc:jdcpool");
  50. }
  51.  
  52.  
  53. public static void logService(String param1, String param2) {
  54. ...
  55. finally {
  56. stmt.close();
  57. }
  58. }
  59.  
  60. public static void closeTrans() {
  61.  
  62. con.commit();
  63. con.close();
  64. }
  65. } //End of DBLog class
  66.  
  67.  
  68. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
  69. ...
  70. finally {
  71. DBLog.closeTrans();
  72. }
  73. }
  74.  
  75. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
  76. Connection conn = obtainConnection(); // obtain connection and store it ThreadLocal
  77. conn.setAutoCommit(false);
  78. try {
  79. chain.doFilter(request, response);
  80. } finally {
  81. conn.commit();
  82. }
  83. }
  84.  
  85. public static void logService(String param1, String param2) {
  86. Connection conn = getConnection(); // obtain thread-local connection
  87. // your database statements go here
  88. }
  89.  
  90. public interface BusinessTask {
  91. public void doWork();
  92. }
  93.  
  94. public class TransactionDecorator implements BusinessTask{
  95.  
  96. private final BusinessTask task;
  97.  
  98. public TransactionDecorator(BusinessTask task){
  99. this.task = task;
  100. }
  101.  
  102. @Override
  103. public void doWork() {
  104. //beging transaction
  105. task.doWork();
  106. //commit or rollback
  107. }
  108. }
  109.  
  110. final AddSavingsAccount addSavingsAccountTask = new AddSavingsAccount();
  111. final TransferFunds transferFundsTask = new TransferFunds();
  112.  
  113. BusinessTask transaction = new TransactionDecorator(new BusinessTask(){
  114.  
  115. @Override
  116. public void doWork() {
  117. addSavingsAccountTask.doWork();
  118. transferFundsTask.doWork();
  119. }
  120. });
  121.  
  122. transaction.doWork(); //all the magic happens here
Add Comment
Please, Sign In to add comment