Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Getting Database Connection using Quartz

I have a requirement where i need to insert data and retrieve the same during my scheduling process.Though i can create my own connection class and can do the work but i am wondering is there a way to obtain a data base connection using Quartz API.

Since Quartz is efficiently doing data base connection and handling so my intention was to use a well defined structure in stead of creating my own.

I saw the following code in the Quartz

conn = DBConnectionManager.getInstance().getConnection(
                    getDataSource());

but i am not sure how good this approach is to obtain the connection.Or is there any good example/resource to create an efficient database connection class.

Quartz Property File

org.quartz.scheduler.instanceName=QuartzScheduler
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.threadPool.threadCount=7
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=root
org.quartz.dataSource.myDS.maxConnections=5

Upvotes: 2

Views: 15881

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

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.

Upvotes: 6

Ngoc Dao
Ngoc Dao

Reputation: 1510

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;
    }
}

Upvotes: 1

Related Questions