org.mybatis.guice.datasource.dbcp.BasicDataSourceProvider Maven / Gradle / Ivy
/*
* Copyright 2009-2022 the original author or authors.
*
* 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
*
* https://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 org.mybatis.guice.datasource.dbcp;
import java.util.Map.Entry;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
/**
* Provides the Apache commons-dbcp {@code BasicDataSource}.
*/
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 driverClassLoader
* ClassLoader to use to load JDBC driver class.
*/
@Inject
public BasicDataSourceProvider(@Named("JDBC.driver") final String driver, @Named("JDBC.url") final String url,
@Named("JDBC.driverClassLoader") final ClassLoader driverClassLoader) {
dataSource.setDriverClassLoader(driverClassLoader);
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
}
/**
* Sets the user.
*
* @param username
* the new user
* @since 3.3
*/
@com.google.inject.Inject(optional = true)
public void setUser(@Named("JDBC.username") final String username) {
dataSource.setUsername(username);
}
/**
* Sets the password.
*
* @param password
* the new password
* @since 3.3
*/
@com.google.inject.Inject(optional = true)
public void setPassword(@Named("JDBC.password") final String password) {
dataSource.setPassword(password);
}
/**
* Sets the auto commit.
*
* @param autoCommit
* the new auto commit
*/
@com.google.inject.Inject(optional = true)
public void setAutoCommit(@Named("JDBC.autoCommit") final boolean autoCommit) {
dataSource.setDefaultAutoCommit(autoCommit);
}
/**
* Sets the driver properties.
*
* @param driverProperties
* the new driver properties
*/
@com.google.inject.Inject(optional = true)
public void setDriverProperties(@Named("JDBC.driverProperties") final Properties driverProperties) {
for (Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy