Skip to main content
summaryrefslogtreecommitdiffstats
blob: 84a16a28ccb724609372806aa1e24903773e7a9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.database.internal.core;

import java.sql.Connection;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.core.IConnectionFactory;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.database.internal.Activator;
import org.eclipse.osee.framework.logging.OseeLog;

public class OseeConnectionPoolImpl {
   private static final int MAX_CONNECTIONS_PER_CLIENT = Math.max(8, 2 * Runtime.getRuntime().availableProcessors());
   private final List<OseeConnectionImpl> connections = new CopyOnWriteArrayList<OseeConnectionImpl>();
   private final String dbUrl;
   private final Properties properties;
   private final ConnectionFactoryProvider connectionFactory;
   private final String driver;

   public OseeConnectionPoolImpl(ConnectionFactoryProvider connectionFactory, String driver, String dbUrl, Properties properties) {
      this.connectionFactory = connectionFactory;
      this.driver = driver;
      this.dbUrl = dbUrl;
      this.properties = properties;
   }

   private IConnectionFactory createConnection(String driver) throws OseeCoreException {
      return connectionFactory.get(driver);
   }

   public synchronized boolean hasOpenConnection() {
      return connections.size() > 0;
   }

   /**
    * at a minimum this should be called on jvm shutdown
    */
   public synchronized void closeConnections() {
      for (OseeConnection connection : connections) {
         connection.close();
      }
      connections.clear();
   }

   synchronized void removeConnection(OseeConnection conn) {
      connections.remove(conn);
   }

   public synchronized OseeConnectionImpl getConnection() throws OseeCoreException {
      for (OseeConnectionImpl connection : connections) {
         if (connection.lease()) {
            return connection;
         }
      }

      if (connections.size() >= MAX_CONNECTIONS_PER_CLIENT) {
         throw new OseeDataStoreException(
            "This client has reached the maximum number of allowed simultaneous database connections of : " + MAX_CONNECTIONS_PER_CLIENT);
      }
      OseeConnectionImpl connection = getOseeConnection();
      connections.add(connection);
      OseeLog.log(Activator.class, Level.INFO, String.format("DbConnection: [%s] - [%d]", dbUrl, connections.size()));
      return connection;
   }

   private OseeConnectionImpl getOseeConnection() throws OseeCoreException {
      try {
         IConnectionFactory connectionDriver = createConnection(driver);
         Connection connection = connectionDriver.getConnection(properties, dbUrl);
         connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
         return new OseeConnectionImpl(connection, this);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   synchronized void returnConnection(OseeConnectionImpl connection) {
      try {
         if (connection.isClosed()) {
            removeConnection(connection);
         } else {
            connection.expireLease();
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
         removeConnection(connection);
      }
   }

   synchronized void releaseUneededConnections() throws OseeCoreException {
      for (OseeConnectionImpl connection : connections) {
         if (connection.isStale()) {
            connection.destroy();
         }
      }
   }
}

Back to the top