-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/modules/core/src/com/haulmont/yarg/util/db/DatasourceCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.haulmont.yarg.util.db; | ||
|
||
import com.haulmont.yarg.exception.InitializationException; | ||
import org.apache.commons.dbcp.*; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* @author degtyarjov | ||
* @version $Id$ | ||
*/ | ||
public final class DatasourceCreator { | ||
private DatasourceCreator() { | ||
} | ||
|
||
public static DataSource setupDataSource(String driver, String connectURI, | ||
String username, | ||
String password, | ||
Integer maxActive, | ||
Integer maxIdle, | ||
Integer maxWait) { | ||
try { | ||
Class.forName(driver); | ||
final AbandonedConfig config = new AbandonedConfig(); | ||
config.setLogAbandoned(true); | ||
|
||
AbandonedObjectPool connectionPool = new AbandonedObjectPool(null, config); | ||
|
||
connectionPool.setMaxIdle(maxIdle); | ||
connectionPool.setMaxActive(maxActive); | ||
if (maxWait != null) { | ||
connectionPool.setMaxWait(maxWait); | ||
} | ||
|
||
ConnectionFactory connectionFactory = | ||
new DriverManagerConnectionFactory(connectURI, username, password); | ||
|
||
PoolableConnectionFactory poolableConnectionFactory = | ||
new PoolableConnectionFactory( | ||
connectionFactory, connectionPool, null, null, false, true); | ||
|
||
connectionPool.setFactory(poolableConnectionFactory); | ||
PoolingDataSource dataSource = | ||
new PoolingDataSource(connectionPool); | ||
|
||
return dataSource; | ||
} catch (ClassNotFoundException e) { | ||
throw new InitializationException("An error occurred during creation of new datasource object", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters