这篇文章将为大家详细讲解有关Hibernate 中Quartz的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
首先实现一个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;
}
}
关于Hibernate 中Quartz的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。