Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
Openfire
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Openfire
Commits
f499120f
Commit
f499120f
authored
Oct 12, 2015
by
Alpha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made connection retry parameters configurable
parent
1f032323
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
46 deletions
+50
-46
DbConnectionManager.java
src/java/org/jivesoftware/database/DbConnectionManager.java
+50
-46
No files found.
src/java/org/jivesoftware/database/DbConnectionManager.java
View file @
f499120f
...
@@ -74,55 +74,59 @@ public class DbConnectionManager {
...
@@ -74,55 +74,59 @@ public class DbConnectionManager {
/** True if the database supports the Statement.setFetchSize()) method. */
/** True if the database supports the Statement.setFetchSize()) method. */
static
boolean
pstmt_fetchSizeSupported
=
true
;
static
boolean
pstmt_fetchSizeSupported
=
true
;
private
static
final
String
SETTING_DATABASE_MAX_RETRIES
=
"database.defaultProvider.maxRetries"
;
private
static
final
String
SETTING_DATABASE_RETRY_DELAY
=
"database.defaultProvider.retryDelay"
;
private
static
DatabaseType
databaseType
=
DatabaseType
.
unknown
;
private
static
DatabaseType
databaseType
=
DatabaseType
.
unknown
;
private
static
SchemaManager
schemaManager
=
new
SchemaManager
();
private
static
SchemaManager
schemaManager
=
new
SchemaManager
();
/**
/**
* Returns a database connection from the currently active connection
* Ensures that the connection provider exists and is set
* provider. An exception will be thrown if no connection was found.
* (auto commit is set to true).
*
* @return a connection.
* @throws SQLException if a SQL exception occurs or no connection was found.
*/
*/
public
static
Connection
getConnection
()
throws
SQLException
{
private
static
void
ensureConnectionProvider
()
{
if
(
connectionProvider
==
null
)
{
if
(
connectionProvider
!=
null
)
return
;
synchronized
(
providerLock
)
{
synchronized
(
providerLock
)
{
if
(
connectionProvider
==
null
)
{
if
(
connectionProvider
!=
null
)
return
;
// Attempt to load the connection provider classname as
//
a Jive property.
// Attempt to load the connection provider classname as
a Jive property.
String
className
=
JiveGlobals
.
getXMLProperty
(
"connectionProvider.className"
);
String
className
=
JiveGlobals
.
getXMLProperty
(
"connectionProvider.className"
);
if
(
className
!=
null
)
{
if
(
className
!=
null
)
{
// Attempt to load the class.
// Attempt to load the class.
try
{
try
{
Class
conClass
=
ClassUtils
.
forName
(
className
);
Class
conClass
=
ClassUtils
.
forName
(
className
);
setConnectionProvider
((
ConnectionProvider
)
conClass
.
newInstance
());
setConnectionProvider
((
ConnectionProvider
)
conClass
.
newInstance
());
}
}
catch
(
Exception
e
)
{
catch
(
Exception
e
)
{
Log
.
warn
(
"Failed to create the "
+
Log
.
warn
(
"Failed to create the "
+
"connection provider specified by connection"
+
"connection provider specified by connection"
+
"Provider.className. Using the default pool."
,
e
);
"Provider.className. Using the default pool."
,
e
);
setConnectionProvider
(
new
DefaultConnectionProvider
());
setConnectionProvider
(
new
DefaultConnectionProvider
());
}
}
}
}
else
{
else
{
setConnectionProvider
(
new
DefaultConnectionProvider
());
setConnectionProvider
(
new
DefaultConnectionProvider
());
}
}
}
}
}
}
}
// TODO: May want to make these settings configurable
/**
Integer
retryCnt
=
0
;
* Returns a database connection from the currently active connection
Integer
retryMax
=
10
;
* provider. An exception will be thrown if no connection was found.
Integer
retryWait
=
250
;
// milliseconds
* (auto commit is set to true).
Connection
con
=
null
;
*
* @return a connection.
* @throws SQLException if a SQL exception occurs or no connection was found.
*/
public
static
Connection
getConnection
()
throws
SQLException
{
ensureConnectionProvider
();
Integer
currentRetryCount
=
0
;
Integer
maxRetries
=
JiveGlobals
.
getIntProperty
(
SETTING_DATABASE_MAX_RETRIES
,
10
);
Integer
retryWait
=
JiveGlobals
.
getIntProperty
(
SETTING_DATABASE_RETRY_DELAY
,
250
);
// milliseconds
SQLException
lastException
=
null
;
SQLException
lastException
=
null
;
do
{
do
{
try
{
try
{
con
=
connectionProvider
.
getConnection
();
Connection
con
=
connectionProvider
.
getConnection
();
if
(
con
!=
null
)
{
if
(
con
!=
null
)
{
// Got one, lets hand it off.
// Got one, lets hand it off.
// Usually profiling is not enabled. So we return a normal
// Usually profiling is not enabled. So we return a normal
...
@@ -130,8 +134,7 @@ public class DbConnectionManager {
...
@@ -130,8 +134,7 @@ public class DbConnectionManager {
// connection with a profiled connection.
// connection with a profiled connection.
if
(!
profilingEnabled
)
{
if
(!
profilingEnabled
)
{
return
con
;
return
con
;
}
}
else
{
else
{
return
new
ProfiledConnection
(
con
);
return
new
ProfiledConnection
(
con
);
}
}
}
}
...
@@ -139,19 +142,20 @@ public class DbConnectionManager {
...
@@ -139,19 +142,20 @@ public class DbConnectionManager {
// TODO distinguish recoverable from non-recoverable exceptions.
// TODO distinguish recoverable from non-recoverable exceptions.
lastException
=
e
;
lastException
=
e
;
Log
.
info
(
"Unable to get a connection from the database pool "
+
Log
.
info
(
"Unable to get a connection from the database pool "
+
"(attempt "
+
retryCnt
+
" out of "
+
retryMax
+
")."
,
e
);
"(attempt "
+
currentRetryCount
+
" out of "
+
maxRetries
+
")."
,
e
);
}
}
try
{
try
{
Thread
.
sleep
(
retryWait
);
Thread
.
sleep
(
retryWait
);
}
catch
(
Exception
e
)
{
// Ignored, the thread was interrupted while waiting, so no need to log either
}
}
catch
(
Exception
e
)
{
currentRetryCount
++;
// Ignored
}
while
(
currentRetryCount
<=
maxRetries
);
}
retryCnt
++;
}
while
(
retryCnt
<=
retryMax
);
throw
new
SQLException
(
"ConnectionManager.getConnection() "
+
throw
new
SQLException
(
"ConnectionManager.getConnection() "
+
"failed to obtain a connection after "
+
retryCnt
+
" retries. "
+
"failed to obtain a connection after "
+
currentRetryCount
+
" retries. "
+
"The exception from the last attempt is as follows: "
+
lastException
);
"The exception from the last attempt is as follows: "
+
lastException
);
}
}
/**
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment