您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。