DatabaseBalancer.java
3.83 KB
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
package it.softecspa.fileproxy;
import it.softecspa.database.dbconnect.ConnectionManager;
import it.softecspa.mvc.businessobject.DatabaseManager;
import org.apache.log4j.Logger;
public class DatabaseBalancer {
protected Logger log = Logger.getLogger(getClass());
public enum Balancer {
STAGE(0)
, MASTER(1);
int i;
private Balancer(int i) {
this.i = i;
}
public int index() {
return i;
}
}
private ConnectionManager[] balance_readwrite = new ConnectionManager[2];
private ConnectionManager[] balance_readonly = new ConnectionManager[2];
private static DatabaseBalancer instance;
private boolean active;
private DatabaseBalancer() {
super();
synchronized (DatabaseBalancer.class) {
if (instance == null) {
instance = this;
instance.load();
}
}
}
public static DatabaseBalancer getInstance() {
synchronized (DatabaseBalancer.class) {
if (instance == null) new DatabaseBalancer();
}
return instance;
}
private synchronized void load() {
active = false;
log.info("Configuring "+this.getClass().getSimpleName());
DatabaseManager dbManager = DatabaseManager.getInstance();
int index = Balancer.STAGE.index();
try {
balance_readwrite[index] = dbManager.getApplicationConnectionManager(0, false);
log.info("- STAGE (read/write) engagged!");
balance_readonly[index] = dbManager.getApplicationConnectionManager(0, true);
log.info("- STAGE (read only) engagged!");
} catch (NullPointerException e) {
// Databse non configurato
log.warn("No database configured!");
return;
}
index = Balancer.MASTER.index();
try {
balance_readwrite[index] = dbManager.getApplicationConnectionManager(1, false);
log.info("- MASTER (read/write) engagged!");
balance_readonly[index] = dbManager.getApplicationConnectionManager(1, true);
log.info("- MASTER (read only) engagged!");
} catch (IndexOutOfBoundsException e) {
// Assimilo MASTER a SLAVE
balance_readwrite[index] = dbManager.getApplicationConnectionManager(0, false);
log.info("- MASTER (read/write) engagged! (the same of STAGE)");
balance_readonly[index] = dbManager.getApplicationConnectionManager(0, true);
log.info("- MASTER (read only) engagged! (the same of STAGE)");
}
active = true;
}
/**
* Restituisce il ConnectionManager in lettura/scrittura
* @return
*/
public ConnectionManager get(Balancer balance) {
return balance_readwrite[balance.index()];
}
public ConnectionManager get(boolean stage) {
return balance_readwrite[stage?Balancer.STAGE.index():Balancer.MASTER.index()];
}
/**
* Restituisce il ConnectionManager in sola lettura
* @return
*/
public ConnectionManager getReadOnly(Balancer balance) {
return balance_readonly[balance.index()];
}
public ConnectionManager getReadOnly(boolean stage) {
return balance_readonly[stage?Balancer.STAGE.index():Balancer.MASTER.index()];
}
/**
* Restituisce il ConnectionManager MASTER in lettura/scrittura
* @return
*/
public ConnectionManager getMaster() {
return balance_readwrite[Balancer.MASTER.index()];
}
/**
* Restituisce il ConnectionManager MASTER in sola lettura
* @return
*/
public ConnectionManager getMasterReadOnly() {
return balance_readonly[Balancer.MASTER.index()];
}
/**
* Restituisce il ConnectionManager di STAGE in lettura/scrittura
* @return
*/
public ConnectionManager getStage() {
return balance_readwrite[Balancer.STAGE.index()];
}
/**
* Restituisce il ConnectionManager di STAGE in sola lettura
* @return
*/
public ConnectionManager getStageReadOnly() {
return balance_readonly[Balancer.STAGE.index()];
}
public boolean isActive() {
return active;
}
}