Alejandro Acuña
2024-07-30 65a64a81d30f00f1fffd5da6866850e1308e1135
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package art.servers.gost.access.types;
 
import art.library.utils.licence.Licence;
import art.library.utils.synchro.Mutex;
import art.servers.Shared;
import art.servers.gost.access.configuration.ConfigurationDetail_Database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
 
 
public class DatabasePoolConnection
{
    private String name = null;
    private ConfigurationDetail_Database configuration = null;
 
    private boolean initialised = false;
    private List<Connection> free = new ArrayList<Connection>();
    private List<Connection> used = new ArrayList<Connection>();
    private Mutex mutexList = new Mutex();
    private Mutex mutexConnection = new Mutex();
 
    
    public DatabasePoolConnection(String name, ConfigurationDetail_Database configuration)
    {
        this.name = name;
        this.configuration = configuration;
        this.initialise();
    }
    
 
    
    public Connection getConnection(boolean autocommit) throws Exception
    {
        mutexConnection.lockWrite();  // Locks until available free connection
        mutexList.lockWrite();
        initialise();  // May be first time there was no connection to database
 
        try
        {
            Connection connection = free.get(0);
            free.remove(connection);
 
            if (connection.isClosed() == true)
            {
                connection = openConnection();
            }
 
            used.add(connection);
            connection.setAutoCommit(autocommit);
            return connection;
        }
        finally
        {
            if (free.size() > 0)
            {
               // We can release only if there are free connections
                
               mutexConnection.releaseWrite();
            }
            
            mutexList.releaseWrite();
        }        
    }
    
        
    
    
    
    public boolean executeUpdate (Connection connection, String sql)
    {
        PreparedStatement statement = null;
 
        try
        {
            statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            return true;
        }
        catch (Exception e)
        {
        }
 
        close(statement);
        return false;
    }
    
    
       
 
    public void vacuum(String tablename, Connection connection) throws Exception
    {
        executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_enabled = true, toast.autovacuum_enabled = true)");
        executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_vacuum_scale_factor = 0.0)");
        executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_vacuum_threshold = 1000)");
        executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_analyze_scale_factor = 0.0)");
        executeUpdate(connection, "ALTER TABLE " + tablename + " SET (autovacuum_analyze_threshold = 1000)");
    }
 
    
    
    
    public void commit(Connection connection) throws Exception
    {
        try
        {
            connection.commit();
        }
        catch (Exception e)
        {
        }
    }    
    
 
    public void rollback(Connection connection) throws Exception
    {
        try
        {
            connection.rollback();
        }
        catch (Exception e)
        {
        }
    }    
        
    
 
    public void close(PreparedStatement statement)
    {
        try
        {
            statement.close();
        }
        catch (Exception e)
        {
        }
    }    
    
    public void close(ResultSet resultset)
    {
        try
        {
            resultset.close();
        }
        catch (Exception e)
        {
        }
    }    
    
    public void close(PreparedStatement statement, ResultSet resultset)
    {
        close(resultset);
        close(statement);
    }    
    
    
    
    public void releaseConnection(Connection connection, PreparedStatement statement)
    {
        close(statement);
        releaseConnection(connection);
    }
    
   
    
    public void releaseConnection(Connection connection, PreparedStatement statement, ResultSet resultset)
    {
        close(resultset);
        close(statement);
        releaseConnection(connection);
    }
    
    
    public void commitConnection(Connection connection, PreparedStatement statement, ResultSet resultset) throws Exception
    {
        close(resultset);
        close(statement);
        connection.commit();
        releaseConnection(connection);
    }    
    
    
    public void rollbackConnection(Connection connection, PreparedStatement statement, ResultSet resultset) throws Exception
    {
        close(resultset);
        close(statement);
        connection.rollback();
        releaseConnection(connection);
    }    
    
 
    public void releaseConnection(Connection connection)
    {
        if (connection == null) return;
        
        mutexList.lockWrite();
        
        try
        {
            used.remove(connection);
            free.add(connection);
        }
        finally
        {
            if (free.size() == 1)
            {
               // Means there were no available connections
               // We can release mutex
                
               mutexConnection.releaseWrite();
            }
 
            mutexList.releaseWrite();
        }
    }    
    
     
    
    public void close()
    {
        mutexList.lockWrite();
        
        try
        {
            for (int i=0; i<free.size(); i++)
            {
                try
                {
                    free.get(i).close();
                }
                catch (Exception e)
                {
                }
            }
            
            
            for (int i=0; i<used.size(); i++)
            {
                try
                {
                    used.get(i).close();
                }
                catch (Exception e)
                {
                }
            }
            
            free.clear();
            used.clear();
        }
        finally
        {
            mutexList.releaseWrite();
        }
    }    
 
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Private">
    
 
    
    private void initialise()
    {
        try
        {
            if (initialised == false)
            {
                for (int i=0; i<configuration.pool; i++)
                {
                    free.add(openConnection());
                }
            }
            
            initialised = true;
        }
        catch (Exception exception)
        {
            Shared.traceError(name, Shared.getMessage("Database pool initialization"), exception); 
            
            for (int i=0; i<free.size(); i++)
            {
                try
                {
                    free.get(i).close();
                }
                catch (Exception e)
                {
                }
            }
            
            free.clear();
        }
    }
        
    private Connection openConnection(ConfigurationDetail_Database configuration, boolean autocommit) throws Exception
    {
        String user =  Licence.decrypt(configuration.user);
        String password =  Licence.decrypt(configuration.password);
        Connection connection = (Connection) DriverManager.getConnection(configuration.connectionString, user, password);
        connection.setAutoCommit(autocommit);     
        return connection;
    }
    
    
    
    private Connection openConnection() throws Exception
    {
        String user =  Licence.decrypt(configuration.user);
        String password =  Licence.decrypt(configuration.password);
        Connection connection = (Connection) DriverManager.getConnection(configuration.connectionString, user, password);
        connection.setAutoCommit(true);     
        return connection;
    }
    
    
    // </editor-fold>
    
    
    
}