com.googlecode.ibaguice.datasource.dbcp.BasicDataSourceProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ibaguice-dbcp13 Show documentation
Show all versions of ibaguice-dbcp13 Show documentation
Apache commons-dbcp Data Source providers
The newest version!
/*
* Copyright 2009-2010 The iBaGuice Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.ibaguice.datasource.dbcp;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Map.Entry;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
/**
* Provides the Apache commons-dbcp {@code BasicDataSource}.
*
* @version $Id: BasicDataSourceProvider.java 2095 2010-06-04 13:56:24Z simone.tripodi $
*/
@Singleton
public final class BasicDataSourceProvider implements Provider {
/**
* The BasicDataSource reference.
*/
private final BasicDataSource dataSource = new BasicDataSource();
/**
* Creates a new BasicDataSource using the needed parameter.
*
* @param driver The JDBC driver class.
* @param url the database URL of the form jdbc:subprotocol:subname
.
* @param username the database user.
* @param password the user's password.
*/
@Inject
public BasicDataSourceProvider(@Named("JDBC.driver") final String driver,
@Named("JDBC.url") final String url,
@Named("JDBC.username") final String username,
@Named("JDBC.password") final String password) {
this.dataSource.setDriverClassName(driver);
this.dataSource.setUrl(url);
this.dataSource.setUsername(username);
this.dataSource.setPassword(password);
}
/**
*
*
* @param autoCommit
*/
@Inject(optional = true)
public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
this.dataSource.setDefaultAutoCommit(autoCommit);
}
/**
*
*
* @param loginTimeout
*/
@Inject(optional = true)
public void setLoginTimeout(@Named("JDBC.loginTimeout") final int loginTimeout) {
try {
this.dataSource.setLoginTimeout(loginTimeout);
} catch (SQLException e) {
throw new RuntimeException("Impossible to set DBCP login timeout '"
+ loginTimeout
+ "', see nested exceptions", e);
}
}
/**
*
* @param driverProperties
*/
@Inject(optional = true)
public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
for (Entry