Getting Database Connection using Quartz

会有一股神秘感。 提交于 2019-12-06 06:40:20

You can get connection utilizing Quartz by naming the data-source you have defined in your property file like

conn = DBConnectionManager.getInstance().getConnection("myDS");

here myDS is the name of the data source you have defined in your property file

but since you are using the underlying data pool of quartz make sure that you close the connection so that it should get back to the pool.

This is just an outline based on my knowledge of Quartz and how it get connection.

If you want to get DataSource:

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.quartz.SchedulerException;
import org.quartz.utils.PoolingConnectionProvider;
import org.quartz.utils.PropertiesParser;

/**
 * This class just exposes the underlying data source backed by C3PO
 * (http://www.mchange.com/projects/c3p0/index.html).
 */
class MyDataSource extends PoolingConnectionProvider {
    public MyDataSource(Properties config) throws SchedulerException, SQLException {
        super(config);
    }

    public DataSource dataSource() {
        return getDataSource();
    }
}

/** This class exposes the data store configured in quartz.properties. */
public class MyDataSourceLoader {
    private static final String DATA_SOURCE_CONFIG = "quartz.properties";
    private static final String DATA_SOURCE_PREFIX = "org.quartz.dataSource.myDS";

    private static final DataSource dataSource;

    static {
        try {
            InputStream in           = Thread.currentThread().getContextClassLoader().getResourceAsStream(DATA_SOURCE_CONFIG);
            Properties  quartzConfig = new Properties();
            quartzConfig.load(in);
            in.close();

            PropertiesParser pp               = new PropertiesParser(quartzConfig);
            Properties       dataSourceConfig = pp.getPropertyGroup(DATA_SOURCE_PREFIX, true);
            MyDataSource     mds              = new MyDataSource(dataSourceConfig);
            dataSource = mds.dataSource();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static DataSource dataSource() {
        return dataSource;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!