001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.lucene.engine.store.jdbc;
018:
019: import java.sql.SQLException;
020: import javax.sql.DataSource;
021:
022: import org.apache.commons.dbcp.BasicDataSource;
023: import org.compass.core.CompassException;
024: import org.compass.core.config.CompassSettings;
025: import org.compass.core.lucene.LuceneEnvironment;
026:
027: /**
028: * A Jakarta Commons Data Source Pool (DBCP) provider.
029: *
030: * @author kimchy
031: */
032: public class DbcpDataSourceProvider extends AbstractDataSourceProvider {
033:
034: protected DataSource doCreateDataSource(String url,
035: CompassSettings settings) throws CompassException {
036: BasicDataSource dataSource = new BasicDataSource();
037: if (!externalAutoCommit) {
038: dataSource.setDefaultAutoCommit(autoCommit);
039: }
040: dataSource.setDriverClassName(driverClass);
041: dataSource.setUrl(url);
042: dataSource.setUsername(username);
043: dataSource.setPassword(password);
044:
045: if (settings
046: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION) != null) {
047: dataSource
048: .setDefaultTransactionIsolation(settings
049: .getSettingAsInt(
050: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.DEFAULT_TRANSACTION_ISOLATION,
051: 0));
052: }
053:
054: if (settings
055: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE) != null) {
056: dataSource
057: .setInitialSize(settings
058: .getSettingAsInt(
059: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.INITIAL_SIZE,
060: 0));
061: }
062:
063: if (settings
064: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE) != null) {
065: dataSource
066: .setMaxActive(settings
067: .getSettingAsInt(
068: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_ACTIVE,
069: 0));
070: }
071:
072: if (settings
073: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE) != null) {
074: dataSource
075: .setMaxIdle(settings
076: .getSettingAsInt(
077: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_IDLE,
078: 0));
079: }
080:
081: if (settings
082: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE) != null) {
083: dataSource
084: .setMinIdle(settings
085: .getSettingAsInt(
086: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MIN_IDLE,
087: 0));
088: }
089:
090: if (settings
091: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT) != null) {
092: dataSource
093: .setMaxWait(settings
094: .getSettingAsLong(
095: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_WAIT,
096: 0));
097: }
098:
099: if (settings
100: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS) != null) {
101: dataSource
102: .setMaxOpenPreparedStatements(settings
103: .getSettingAsInt(
104: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.MAX_OPEN_PREPARED_STATEMENTS,
105: 0));
106: }
107:
108: if (settings
109: .getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS) != null) {
110: dataSource
111: .setPoolPreparedStatements(settings
112: .getSettingAsBoolean(
113: LuceneEnvironment.JdbcStore.DataSourceProvider.Dbcp.POOL_PREPARED_STATEMENTS,
114: false));
115: }
116:
117: return dataSource;
118: }
119:
120: public void closeDataSource() {
121: try {
122: ((BasicDataSource) getDataSource()).close();
123: } catch (SQLException e) {
124: // do nothing
125: }
126: }
127: }
|