Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. ExecutorService executor = Executors.newFixedThreadPool(20);
  2. DBConnectDev dbConnectdb=null;
  3.  
  4. try{
  5. List<Runnable> tasks = new ArrayList<>();
  6.  
  7.  
  8. xxtList.forEach(xxDVO -> {
  9.  
  10. Runnable runnable = new Runnable() {
  11. public void run() {
  12.  
  13. ThreadProcess.controlProcess(dbConnectdb,xxDVO,Thread.currentThread().getId());
  14. }
  15. };
  16. tasks.add(runnable);
  17. });
  18.  
  19. CompletableFuture<?>[] futures = tasks.stream().map(task -> CompletableFuture.runAsync(task, executor))
  20. .toArray(CompletableFuture[]::new);
  21. CompletableFuture.allOf(futures).join();
  22.  
  23. if(Globals.THREAD_COUNT>0){
  24. Globals.THREAD_COUNT=Globals.THREAD_COUNT-1;
  25.  
  26. }
  27. executor.shutdown();
  28. executor.awaitTermination(3, TimeUnit.SECONDS);
  29.  
  30. } catch (InterruptedException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. dbConnectdb.closeAllConnection();
  35.  
  36. }
  37.  
  38. public static void spasKontrolIslem(DBConnectDev dbConnectionManager){
  39.  
  40.  
  41. Connection connection=null;
  42.  
  43. try {
  44.  
  45. connection = dbConnectionManager.getConnectionFromPool();
  46. //database processes
  47.  
  48. } catch (Exception e) {
  49.  
  50. e.printStackTrace();
  51. }
  52. finally {
  53. if (connection != null)
  54. dbConnectionManager.returnConnectionToPool(connection);
  55. }
  56.  
  57.  
  58.  
  59. }
  60.  
  61. public class DBConnectDev {String driver = "com.ibm.db2.jcc.DB2Driver";
  62.  
  63. final int MAX_POOL_SIZE = 20;
  64. Vector<Connection> connectionPool = new Vector<>();
  65.  
  66. public DBConnectDev() {
  67. initialize();
  68. }
  69.  
  70. private void initialize() {
  71. for (int i = 0; i < MAX_POOL_SIZE; i++)
  72. initializeConnectionPool();
  73. }
  74.  
  75. private void initializeConnectionPool() {
  76. while (!checkIfConnectionPoolIsFull()) {
  77. connectionPool.addElement(createNewConnectionForPool());
  78. }
  79. }
  80.  
  81. private synchronized boolean checkIfConnectionPoolIsFull() {
  82. if (connectionPool.size() < MAX_POOL_SIZE)
  83. return false;
  84. return true;
  85. }
  86.  
  87. private Connection createNewConnectionForPool() {
  88. Connection connection=null;
  89.  
  90. try {
  91. Context ctx=new InitialContext();
  92. DataSource ds = (DataSource)
  93. ctx.lookup("JNDINAME");
  94.  
  95. connection=(Connection) ds.getConnection();
  96.  
  97. } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) {
  98. // TODO Auto-generated catch block
  99. e.printStackTrace();
  100. }
  101.  
  102. return connection;
  103. }
  104.  
  105. public synchronized Connection getConnectionFromPool() {
  106. Connection connection = null;
  107.  
  108. if (connectionPool.size() > 0) {
  109. connection = (Connection) connectionPool.firstElement();
  110. connectionPool.removeElementAt(0);
  111. }
  112. return connection;
  113. }
  114.  
  115. public synchronized void returnConnectionToPool(Connection connection) {
  116. connectionPool.addElement(connection);
  117. }
  118.  
  119. public void closeAllConnection(){
  120.  
  121. if (connectionPool.size() > 0) {
  122. connectionPool.forEach(connection -> {
  123. try {
  124. ((Connection)connection).close();
  125. } catch (SQLException e) {
  126. System.out.println("dev Connection kapatmada hata oluştu");
  127. }
  128. });
  129. connectionPool.removeAllElements();
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement