com.adviser.osgi.db.datasource.base.IPojoPoolingDataSource.java Source code

Java tutorial

Introduction

Here is the source code for com.adviser.osgi.db.datasource.base.IPojoPoolingDataSource.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.adviser.osgi.db.datasource.base;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Validate;

/**
 *
 * @author menabe
 */
@Component
@Provides
public class IPojoPoolingDataSource implements DataSource {

    private DataSource poolingDataSource;
    @Property
    private DataSourceParams dataSourceParams;

    @Validate
    public void start() {
        poolingDataSource = new PoolingDataSource(dataSourceParams.poolableConnectionFactory.getPool());
    }

    @Invalidate
    public void stop() {
        poolingDataSource = null;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return poolingDataSource.getConnection();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return poolingDataSource.getConnection(username, password);
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return poolingDataSource.getLogWriter();
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {
        poolingDataSource.setLogWriter(out);
    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {
        poolingDataSource.setLoginTimeout(seconds);
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return poolingDataSource.getLoginTimeout();
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return poolingDataSource.getParentLogger();
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return poolingDataSource.unwrap(iface);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return poolingDataSource.isWrapperFor(iface);
    }

}