程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Quartz與Hibernate數據庫連接的結合

Quartz與Hibernate數據庫連接的結合

編輯:關於JAVA

首先介紹一下我們的系統構架,采用webwork,spring,hibernate整合架構,現在引入Quartz框架,Quartz和 hibernate各自擁有的自己數據庫(數據庫培訓 數據庫認證 )配置和連接池的配置,現在的任務是把兩者用同一套配置.

先談談Quartz,我用的版本是1.5.1,後面的版本我沒去看了,沒時間,接下來說的都以這個版本為准.Quartz雖然在提供一個 ConnectionProvider接口,提供給程序擴展,但是看它的StdSchedulerFactory實現類,要對它進行擴展還是一件比較麻煩一件事情,所以我就直接修改了StdSchedulerFactory類,以達到的我的需求.

接下來的問題是既然要結合,那麼讓Quartz來提供連接池配置呢還是有Hibernate提供.研究了兩者的連接池方面的源代碼,發現 Hibernate的連接池設計遠遠好於Quartz,Quartz目前只提供一個連接池提供類PoolingConnectionProvider,而且使用的連接池是dbcp,這個連接池聽說問題多多,相反hibernate提供了更加靈活的配置,所以連接池由Hibernate提供.

首先實現一個Quartz中ConnectionProvider子類HibernateConnectionProvider

public class HibernateConnectionProvider implements ConnectionProvider {

private Connection con = null;

protected static ThreadLocal hibernateHolder = new ThreadLocal();

public Connection getConnection() throws SQLException {

con = null;

SessionFactoryImpl sessionFactory = null;

sessionFactory = (SessionFactoryImpl) Global.getSessionFacotry();

con = sessionFactory.getConnectionProvider().getConnection();

return con;

}

public void shutdown() throws SQLException {

// FIXME Auto-generated method stub

}

}

,然後修改StdSchedulerFactory,在不破壞其本來的邏輯和可配置性下進行了相應的修改.修改的是private Scheduler instantiate() throws SchedulerException方法,找到//Set up any DataSources段進行修改,代碼如下:

String[] dsNames = cfg.getPropertyGroups(PROP_DATASOURCE_PREFIX);

for (int i = 0; i < dsNames.length; i++) {

PropertiesParser pp = new PropertIEsParser(cfg.getPropertyGroup(

PROP_DATASOURCE_PREFIX + "." + dsNames[i], true));

String dsDriver = pp

.getStringProperty(PROP_DATASOURCE_DRIVER, null);

String dsURL = pp.getStringProperty(PROP_DATASOURCE_URL, null);

boolean dsAlwaysLookup = pp.getBooleanProperty(

PROP_DATASOURCE_JNDI_ALWAYS_LOOKUP, false);

String dsUser = pp.getStringProperty(PROP_DATASOURCE_USER, "");

String dsPass = pp.getStringProperty(PROP_DATASOURCE_PASSWord, "");

int dsCnt = pp.getIntProperty(PROP_DATASOURCE_MAX_CONNECTIONS, 3);

String providerClass = pp.getStringProperty(

PROP_DATASOURCE_PROVIDER_CLASS, null);

String dsJndi = pp

.getStringProperty(PROP_DATASOURCE_JNDI_URL, null);

String dsJndiInitial = pp.getStringProperty(

PROP_DATASOURCE_JNDI_INITIAL, null);

String dsJndiProvider = pp.getStringProperty(

PROP_DATASOURCE_JNDI_PROVDER, null);

String dsJndiPrincipal = pp.getStringProperty(

PROP_DATASOURCE_JNDI_PRINCIPAL, null);

String dsJndiCredentials = pp.getStringProperty(

PROP_DATASOURCE_JNDI_CREDENTIALS, null);

String dsValidation = pp.getStringProperty(

PROP_DATASOURCE_VALIDATION_QUERY, null);

if (providerClass == null

|| providerClass.equals("")

|| providerClass

.equals("org.quartz.utils.PoolingConnectionProvider")) {

if (dsDriver == null) {

initException = new SchedulerException(

"Driver not specifIEd for DataSource: "

+ dsNames[i]);

throw initException;

}

if (dsURL == null) {

initException = new SchedulerException(

"DB URL not specifIEd for DataSource: "

+ dsNames[i]);

throw initException;

}

try {

PoolingConnectionProvider cp = new PoolingConnectionProvider(

dsDriver, dsURL, dsUser, dsPass, dsCnt,

dsValidation);

dbMgr = DBConnectionManager.getInstance();

dbMgr.addConnectionProvider(dsNames[i], cp);

} catch (SQLException sqle) {

initException = new SchedulerException(

"Could not initialize DataSource: " + dsNames[i],

sqle);

throw initException;

}

} else if (providerClass

.equals("org.quartz.utils.JNDIConnectionProvider")) {

PropertIEs props = null;

if (null != dsJndiInitial || null != dsJndiProvider

|| null != dsJndiPrincipal || null != dsJndiCredentials) {

props = new PropertIEs();

if (dsJndiInitial != null)

props.put(PROP_DATASOURCE_JNDI_INITIAL, dsJndiInitial);

if (dsJndiProvider != null)

props.put(PROP_DATASOURCE_JNDI_PROVDER, dsJndiProvider);

if (dsJndiPrincipal != null)

props.put(PROP_DATASOURCE_JNDI_PRINCIPAL,

dsJndiPrincipal);

if (dsJndiCredentials != null)

props.put(PROP_DATASOURCE_JNDI_CREDENTIALS,

dsJndiCredentials);

}

JNDIConnectionProvider cp = new JNDIConnectionProvider(dsJndi,

props, dsAlwaysLookup);

dbMgr = DBConnectionManager.getInstance();

dbMgr.addConnectionProvider(dsNames[i], cp);

} else if (providerClass

.equals("org.quartz.utils.HibernateConnectionProvider")) {

HibernateConnectionProvider cp = new HibernateConnectionProvider();

dbMgr = DBConnectionManager.getInstance();

dbMgr.addConnectionProvider(dsNames[i], cp);

} else {

initException = new SchedulerException(

"Provider_Class not Supported: " + providerClass);

throw initException;

}

}

最後就是修改配置文件quartz.propertIEs,加入

#org.quartz.dataSource.myDS.provider_class = org.quartz.utils.JNDIConnectionProvider

#org.quartz.dataSource.myDS.provider_class = org.quartz.utils.PoolingConnectionProvider

org.quartz.dataSource.myDS.provider_class = org.quartz.utils.HibernateConnectionProvider加#的供參考.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved