Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExecutorService executor = Executors.newFixedThreadPool(20);
- DBConnectDev dbConnectdb=null;
- try{
- List<Runnable> tasks = new ArrayList<>();
- xxtList.forEach(xxDVO -> {
- Runnable runnable = new Runnable() {
- public void run() {
- ThreadProcess.controlProcess(dbConnectdb,xxDVO,Thread.currentThread().getId());
- }
- };
- tasks.add(runnable);
- });
- CompletableFuture<?>[] futures = tasks.stream().map(task -> CompletableFuture.runAsync(task, executor))
- .toArray(CompletableFuture[]::new);
- CompletableFuture.allOf(futures).join();
- if(Globals.THREAD_COUNT>0){
- Globals.THREAD_COUNT=Globals.THREAD_COUNT-1;
- }
- executor.shutdown();
- executor.awaitTermination(3, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- dbConnectdb.closeAllConnection();
- }
- public static void spasKontrolIslem(DBConnectDev dbConnectionManager){
- Connection connection=null;
- try {
- connection = dbConnectionManager.getConnectionFromPool();
- //database processes
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally {
- if (connection != null)
- dbConnectionManager.returnConnectionToPool(connection);
- }
- }
- public class DBConnectDev {String driver = "com.ibm.db2.jcc.DB2Driver";
- final int MAX_POOL_SIZE = 20;
- Vector<Connection> connectionPool = new Vector<>();
- public DBConnectDev() {
- initialize();
- }
- private void initialize() {
- for (int i = 0; i < MAX_POOL_SIZE; i++)
- initializeConnectionPool();
- }
- private void initializeConnectionPool() {
- while (!checkIfConnectionPoolIsFull()) {
- connectionPool.addElement(createNewConnectionForPool());
- }
- }
- private synchronized boolean checkIfConnectionPoolIsFull() {
- if (connectionPool.size() < MAX_POOL_SIZE)
- return false;
- return true;
- }
- private Connection createNewConnectionForPool() {
- Connection connection=null;
- try {
- Context ctx=new InitialContext();
- DataSource ds = (DataSource)
- ctx.lookup("JNDINAME");
- connection=(Connection) ds.getConnection();
- } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return connection;
- }
- public synchronized Connection getConnectionFromPool() {
- Connection connection = null;
- if (connectionPool.size() > 0) {
- connection = (Connection) connectionPool.firstElement();
- connectionPool.removeElementAt(0);
- }
- return connection;
- }
- public synchronized void returnConnectionToPool(Connection connection) {
- connectionPool.addElement(connection);
- }
- public void closeAllConnection(){
- if (connectionPool.size() > 0) {
- connectionPool.forEach(connection -> {
- try {
- ((Connection)connection).close();
- } catch (SQLException e) {
- System.out.println("dev Connection kapatmada hata oluştu");
- }
- });
- connectionPool.removeAllElements();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement