Skip to content

Commit cc3dce6

Browse files
committed
#46 - Refactor reduce Loggers to single common "io.ebean.datasource"
1 parent c5b632d commit cc3dce6

File tree

8 files changed

+60
-71
lines changed

8 files changed

+60
-71
lines changed

ebean-datasource-api/src/main/java/io/ebean/datasource/PostgresInitDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class PostgresInitDatabase implements InitDatabase {
1414

15-
private static final Logger log = LoggerFactory.getLogger(PostgresInitDatabase.class);
15+
private static final Logger log = LoggerFactory.getLogger("io.ebean.datasource");
1616

1717
@Override
1818
public void run(Connection connection, DataSourceConfig config) throws SQLException {

ebean-datasource/src/main/java/io/ebean/datasource/pool/BusyConnectionBuffer.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.ebean.datasource.pool;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
5-
63
import java.util.Arrays;
74

85
/**
@@ -16,8 +13,6 @@
1613
*/
1714
final class BusyConnectionBuffer {
1815

19-
private static final Logger logger = LoggerFactory.getLogger(BusyConnectionBuffer.class);
20-
2116
private PooledConnection[] slots;
2217
private final int growBy;
2318
private int size;
@@ -78,7 +73,7 @@ boolean remove(PooledConnection pc) {
7873
int slotId = pc.getSlotId();
7974
if (slots[slotId] != pc) {
8075
PooledConnection heldBy = slots[slotId];
81-
logger.warn("Failed to remove from slot[{}] PooledConnection[{}] - HeldBy[{}]", pc.getSlotId(), pc, heldBy);
76+
Log.log.warn("Failed to remove from slot[{}] PooledConnection[{}] - HeldBy[{}]", pc.getSlotId(), pc, heldBy);
8277
return false;
8378
}
8479
slots[slotId] = null;
@@ -91,7 +86,7 @@ boolean remove(PooledConnection pc) {
9186
*/
9287
void closeBusyConnections(long leakTimeMinutes) {
9388
long olderThanTime = System.currentTimeMillis() - (leakTimeMinutes * 60000);
94-
logger.debug("Closing busy connections using leakTimeMinutes {}", leakTimeMinutes);
89+
Log.log.debug("Closing busy connections using leakTimeMinutes {}", leakTimeMinutes);
9590
for (int i = 0; i < slots.length; i++) {
9691
if (slots[i] != null) {
9792
//tmp.add(slots[i]);
@@ -111,11 +106,11 @@ void closeBusyConnections(long leakTimeMinutes) {
111106

112107
private void closeBusyConnection(PooledConnection pc) {
113108
try {
114-
logger.warn("DataSourcePool closing busy connection? " + pc.getFullDescription());
109+
Log.log.warn("DataSourcePool closing busy connection? " + pc.getFullDescription());
115110
System.out.println("CLOSING busy connection: " + pc.getFullDescription());
116111
pc.closeConnectionFully(false);
117112
} catch (Exception ex) {
118-
logger.error("Error when closing potentially leaked connection " + pc.getDescription(), ex);
113+
Log.log.error("Error when closing potentially leaked connection " + pc.getDescription(), ex);
119114
}
120115
}
121116

@@ -124,13 +119,13 @@ private void closeBusyConnection(PooledConnection pc) {
124119
*/
125120
String getBusyConnectionInformation(boolean toLogger) {
126121
if (toLogger) {
127-
logger.info("Dumping [{}] busy connections: (Use datasource.xxx.capturestacktrace=true ... to get stackTraces)", size());
122+
Log.log.info("Dumping [{}] busy connections: (Use datasource.xxx.capturestacktrace=true ... to get stackTraces)", size());
128123
}
129124
StringBuilder sb = new StringBuilder();
130125
for (PooledConnection pc : slots) {
131126
if (pc != null) {
132127
if (toLogger) {
133-
logger.info("Busy Connection - {}", pc.getFullDescription());
128+
Log.log.info("Busy Connection - {}", pc.getFullDescription());
134129
} else {
135130
sb.append(pc.getFullDescription()).append("\r\n");
136131
}

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.ebean.datasource.InitDatabase;
1010
import io.ebean.datasource.PoolStatus;
1111
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
1312

1413
import java.io.PrintWriter;
1514
import java.sql.Connection;
@@ -42,7 +41,7 @@
4241
*/
4342
public final class ConnectionPool implements DataSourcePool {
4443

45-
private static final Logger logger = LoggerFactory.getLogger(ConnectionPool.class);
44+
private static final Logger log = Log.log;
4645

4746
private final ReentrantLock heartbeatLock = new ReentrantLock(false);
4847
private final ReentrantLock notifyLock = new ReentrantLock(false);
@@ -200,12 +199,12 @@ private void initialiseConnections() throws SQLException {
200199
"] min[" + minConnections +
201200
"] max[" + maxConnections +
202201
"] in[" + (System.currentTimeMillis() - start) + "ms]";
203-
logger.info(msg);
202+
log.info(msg);
204203
} catch (SQLException e) {
205204
if (failOnStart) {
206205
throw e;
207206
}
208-
logger.error("Error trying to ensure minimum connections, maybe db server is down - message:" + e.getMessage(), e);
207+
log.error("Error trying to ensure minimum connections, maybe db server is down - message:" + e.getMessage(), e);
209208
}
210209
}
211210

@@ -219,7 +218,7 @@ private void initialiseDatabase() throws SQLException {
219218
// successfully obtained a connection so skip initDatabase
220219
connection.clearWarnings();
221220
} catch (SQLException e) {
222-
logger.info("Obtaining connection using ownerUsername:{} to initialise database", config.getOwnerUsername());
221+
log.info("Obtaining connection using ownerUsername:{} to initialise database", config.getOwnerUsername());
223222
// expected when user does not exist, obtain a connection using owner credentials
224223
try (Connection ownerConnection = createUnpooledConnection(config.getOwnerUsername(), config.getOwnerPassword())) {
225224
// initialise the DB (typically create the user/role using the owner credentials etc)
@@ -304,7 +303,7 @@ public SQLException getDataSourceDownReason() {
304303
void notifyWarning(String msg) {
305304
if (inWarningMode.compareAndSet(false, true)) {
306305
// send an Error to the event log...
307-
logger.warn(msg);
306+
log.warn(msg);
308307
if (notify != null) {
309308
notify.dataSourceWarning(this, msg);
310309
}
@@ -325,7 +324,7 @@ private void notifyDown(SQLException reason) {
325324
// check and set false immediately so that we only alert once
326325
dataSourceUp.set(false);
327326
dataSourceDownReason = reason;
328-
logger.error("FATAL: DataSourcePool [" + name + "] is down or has network error!!!", reason);
327+
log.error("FATAL: DataSourcePool [" + name + "] is down or has network error!!!", reason);
329328
if (notify != null) {
330329
notify.dataSourceDown(this, reason);
331330
}
@@ -350,12 +349,12 @@ private void notifyUp() {
350349
dataSourceUp.set(true);
351350
startHeartBeatIfStopped();
352351
dataSourceDownReason = null;
353-
logger.error("RESOLVED FATAL: DataSourcePool [" + name + "] is back up!");
352+
log.error("RESOLVED FATAL: DataSourcePool [" + name + "] is back up!");
354353
if (notify != null) {
355354
notify.dataSourceUp(this);
356355
}
357356
} else {
358-
logger.info("DataSourcePool [" + name + "] is back up!");
357+
log.info("DataSourcePool [{}] is back up!", name);
359358
}
360359
} finally {
361360
notifyLock.unlock();
@@ -371,7 +370,7 @@ private void trimIdleConnections() {
371370
queue.trim(maxInactiveMillis, maxAgeMillis);
372371
lastTrimTime = System.currentTimeMillis();
373372
} catch (Exception e) {
374-
logger.error("Error trying to trim idle connections - message:" + e.getMessage(), e);
373+
log.error("Error trying to trim idle connections - message:" + e.getMessage(), e);
375374
}
376375
}
377376
}
@@ -401,7 +400,7 @@ private void checkDataSource() {
401400
conn.close();
402401
}
403402
} catch (SQLException ex) {
404-
logger.warn("Can't close connection in checkDataSource!");
403+
log.warn("Can't close connection in checkDataSource!");
405404
}
406405
}
407406
}
@@ -556,14 +555,14 @@ private boolean testConnection(Connection conn) throws SQLException {
556555
rset.close();
557556
}
558557
} catch (SQLException e) {
559-
logger.error("Error closing resultSet", e);
558+
log.error("Error closing resultSet", e);
560559
}
561560
try {
562561
if (stmt != null) {
563562
stmt.close();
564563
}
565564
} catch (SQLException e) {
566-
logger.error("Error closing statement", e);
565+
log.error("Error closing statement", e);
567566
}
568567
}
569568
}
@@ -575,7 +574,7 @@ boolean validateConnection(PooledConnection conn) {
575574
try {
576575
return testConnection(conn);
577576
} catch (Exception e) {
578-
logger.warn("heartbeatsql test failed on connection:" + conn.getName() + " message:" + e.getMessage());
577+
log.warn("Heartbeat test failed on connection:" + conn.getName() + " message:" + e.getMessage());
579578
return false;
580579
}
581580
}
@@ -617,7 +616,7 @@ private void returnTheConnection(PooledConnection pooledConnection, boolean forc
617616

618617
void returnConnectionReset(PooledConnection pooledConnection) {
619618
queue.returnPooledConnection(pooledConnection, true);
620-
logger.warn("Resetting DataSourcePool on read-only failure [{}]", name);
619+
log.warn("Resetting DataSourcePool on read-only failure [{}]", name);
621620
reset();
622621
}
623622

@@ -739,7 +738,7 @@ public void offline() {
739738
private void shutdownPool(boolean closeBusyConnections) {
740739
stopHeartBeatIfRunning();
741740
PoolStatus status = queue.shutdown(closeBusyConnections);
742-
logger.info("DataSourcePool [{}] shutdown {} psc[hit:{} miss:{} put:{} rem:{}]", name, status, pscHit, pscMiss, pscPut, pscRem);
741+
log.info("DataSourcePool [{}] shutdown {} psc[hit:{} miss:{} put:{} rem:{}]", name, status, pscHit, pscMiss, pscPut, pscRem);
743742
dataSourceUp.set(false);
744743
}
745744

ebean-datasource/src/main/java/io/ebean/datasource/pool/FreeConnectionBuffer.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package io.ebean.datasource.pool;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
5-
63
import java.util.ArrayList;
74
import java.util.Iterator;
85
import java.util.LinkedList;
@@ -16,8 +13,6 @@
1613
*/
1714
final class FreeConnectionBuffer {
1815

19-
private static final Logger logger = LoggerFactory.getLogger(FreeConnectionBuffer.class);
20-
2116
/**
2217
* Buffer oriented for add and remove.
2318
*/
@@ -60,7 +55,7 @@ PooledConnection remove() {
6055
void closeAll(boolean logErrors) {
6156
List<PooledConnection> tempList = new ArrayList<>(freeBuffer);
6257
freeBuffer.clear();
63-
logger.debug("... closing all {} connections from the free list with logErrors: {}", tempList.size(), logErrors);
58+
Log.log.trace("... closing all {} connections from the free list with logErrors: {}", tempList.size(), logErrors);
6459
for (PooledConnection connection : tempList) {
6560
connection.closeConnectionFully(logErrors);
6661
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.ebean.datasource.pool;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* Common Logger instance for datasource implementation.
8+
*/
9+
final class Log {
10+
11+
static final Logger log = LoggerFactory.getLogger("io.ebean.datasource");
12+
13+
}

ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
final class PooledConnection extends ConnectionDelegator {
3232

33-
private static final Logger logger = LoggerFactory.getLogger(PooledConnection.class);
33+
private static final Logger log = Log.log;
3434

3535
private static final String IDLE_CONNECTION_ACCESSED_ERROR = "Pooled Connection has been accessed whilst idle in the pool, via method: ";
3636

@@ -211,22 +211,22 @@ public void setLongRunning(boolean longRunning) {
211211
* @param logErrors if false then don't log errors when closing
212212
*/
213213
void closeConnectionFully(boolean logErrors) {
214-
if (logger.isDebugEnabled()) {
215-
logger.debug("Closing Connection[{}] slot[{}] reason[{}], pstmtStats: {} ", name, slotId, closeReason, pstmtCache.description());
214+
if (log.isTraceEnabled()) {
215+
log.trace("Closing Connection[{}] slot[{}] reason[{}], pstmtStats: {} ", name, slotId, closeReason, pstmtCache.description());
216216
}
217217
if (pool != null) {
218218
pool.pstmtCacheMetrics(pstmtCache);
219219
}
220220
try {
221221
if (connection.isClosed()) {
222-
// Typically the JDBC Driver has its own JVM shutdown hook and already
222+
// Typically, the JDBC Driver has its own JVM shutdown hook and already
223223
// closed the connections in our DataSource pool so making this DEBUG level
224-
logger.debug("Closing Connection[{}] that is already closed?", name);
224+
log.trace("Closing Connection[{}] that is already closed?", name);
225225
return;
226226
}
227227
} catch (SQLException ex) {
228228
if (logErrors) {
229-
logger.error("Error checking if connection [" + name + "] is closed", ex);
229+
log.error("Error checking if connection [" + name + "] is closed", ex);
230230
}
231231
}
232232
try {
@@ -235,15 +235,15 @@ void closeConnectionFully(boolean logErrors) {
235235
}
236236
} catch (SQLException ex) {
237237
if (logErrors) {
238-
logger.warn("Error when closing connection Statements", ex);
238+
log.warn("Error when closing connection Statements", ex);
239239
}
240240
}
241241
try {
242242
connection.close();
243243
pool.dec();
244244
} catch (SQLException ex) {
245-
if (logErrors || logger.isDebugEnabled()) {
246-
logger.error("Error when fully closing connection [" + getFullDescription() + "]", ex);
245+
if (logErrors || log.isDebugEnabled()) {
246+
log.error("Error when fully closing connection [" + getFullDescription() + "]", ex);
247247
}
248248
}
249249
}
@@ -290,7 +290,7 @@ void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
290290
// Already an entry in the cache with the exact same SQL...
291291
pstmt.closeDestroy();
292292
} catch (SQLException e) {
293-
logger.error("Error closing Pstmt", e);
293+
log.error("Error closing PreparedStatement", e);
294294
}
295295
}
296296
} finally {
@@ -447,7 +447,7 @@ public void close() throws SQLException {
447447

448448
} catch (Exception ex) {
449449
// the connection is BAD, remove it, close it and test the pool
450-
logger.warn("Error when trying to return connection to pool, closing fully.", ex);
450+
log.warn("Error when trying to return connection to pool, closing fully.", ex);
451451
pool.returnConnectionForceClose(this);
452452
}
453453
}
@@ -467,11 +467,11 @@ protected void finalize() throws Throwable {
467467
try {
468468
if (connection != null && !connection.isClosed()) {
469469
// connect leak?
470-
logger.warn("Closing Connection on finalize() - {}", getFullDescription());
470+
log.warn("Closing Connection on finalize() - {}", getFullDescription());
471471
closeConnectionFully(false);
472472
}
473473
} catch (Exception e) {
474-
logger.error("Error when finalize is closing a connection? (unexpected)", e);
474+
log.error("Error when finalize is closing a connection? (unexpected)", e);
475475
}
476476
super.finalize();
477477
}

0 commit comments

Comments
 (0)