Quzrtz.net-sqlite 資料庫持久化配置

草青工作室發表於2016-12-22

Quzrtz.net-sqlite 資料庫持久化配置

官方配置持久化資料:http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/job-stores.html

持久化建庫指令碼位置:在下載的 quartz 原始碼 database/dbtables/tables_sqlite.sql 位置,使用 SqliteStudio 工具執行就行了


1.下載原始碼(我下載的是 2.3.3.0 版本)


2.開啟專案,找到 dbproviders.properties 檔案 ,該檔案是個嵌入資源
  位置:Quartz\Impl\AdoJobStore\Common\dbproviders.properties


3.發現 quartz  2.3.3.0 支援 System.Data.SQLite.dll(1.0.88.0) 的驅動


4.檢視自己的引用的(System.Data.SQLite.dll)是 1.0.102.0 版本,所以需要新加一個驅動描述。如下:

# Sqlite3.0  provider
quartz.dbprovider.SQLite-10102.assemblyName=System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.connectionType=System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.commandType=System.Data.SQLite.SQLiteCommand, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.parameterType=System.Data.SQLite.SQLiteParameter, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.commandBuilderType=System.Data.SQLite.SQLiteCommandBuilder, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.parameterDbType=System.Data.SQLite.TypeAffinity, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.parameterDbTypePropertyName=DbType
quartz.dbprovider.SQLite-10102.parameterNamePrefix=@
quartz.dbprovider.SQLite-10102.exceptionType=System.Data.SQLite.SQLiteException, System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139
quartz.dbprovider.SQLite-10102.useParameterNamePrefixInParameterCollection=true
quartz.dbprovider.SQLite-10102.bindByName=true

5.編寫一個函式在初始化 quartz 前呼叫

#region 資料庫持久化job        
private static IScheduler _sched = null;
/// <summary>
/// 初始化資料庫儲存
/// </summary>
public void InitDbStore()
{
	//持久化配置參考
	//http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/job-stores.html
	//新增驅動型別
	//位置:\Quartz\Impl\AdoJobStore\Common\dbproviders.properties
	//1.首先建立一個作業排程池
	var properties = new NameValueCollection();
	//儲存型別
	properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
	//表明字首
	properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
	//驅動型別
	properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz";
	//資料來源名稱
	string DBName = "quartz_storage";
	string DbPath = Grass.Config.ConfigHelper.GetAppSettingsValue("quartz_storage"); //獲取config中 quartz_storage 節點內容
	//資料來源名稱
	properties["quartz.jobStore.dataSource"] = DBName;
	//拼接連線屬性名稱
	string conPropertieName = string.Format("quartz.dataSource.{0}.connectionString", DBName);
	//連線字串
	properties[conPropertieName] = string.Format("Data Source={0};Pooling=true;FailIfMissing=false;Version=3;UTF8Encoding=True;Journal Mode=DELETE;", DbPath);
	//拼接驅動屬性名稱
	string proPropertieName = string.Format("quartz.dataSource.{0}.provider", DBName);
	properties[proPropertieName] = "SQLite-10102"; //新加的驅動名稱
	//Configuring AdoJobStore to use strings as JobDataMap values (recommended)
	properties["quartz.jobStore.useProperties"] = "true";

	ISchedulerFactory sf = new StdSchedulerFactory(properties);
	_sched = sf.GetScheduler();
}
#endregion

6.配置config appsettings
  <add key="quartz_storage" value="E:\SqliteDb\quartz_storage.db"/>

相關文章