Quartz与Hibernate数据库连接的结合
2008-01-05 10:26:32 来源:WEB开发网核心提示: 首先介绍一下我们的系统构架,采用webwork,Quartz与Hibernate数据库连接的结合,sPRing,hibernate整合架构,现在引入Quartz框架,Quartz和 hibernate各自拥有的自己数据库配置和连接池的配置,现在的任务是把两者用同一套配置.先谈谈Qu
首先介绍一下我们的系统构架,采用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加#的供参考.
更多精彩
赞助商链接