iBATIS.NET連接數(shù)據(jù)庫處理淺析
在iBATIS.NET中,很多操作都被隱藏起來了,比如對(duì)數(shù)據(jù)庫的連接和事務(wù)處理,都在框架中處理了。那么框架中具體是怎么處理的呢?有沒有值得借鑒的地方?我們能不能跳過框架自己處理呢?
iBATIS.NET連接數(shù)據(jù)庫是如何呢?首先我們看一下在iBATIS.NET中的一個(gè)常規(guī)的數(shù)據(jù)庫查詢操作:
AccountBusiness的調(diào)用代碼:
- //取得靜態(tài)的DaoManager
- IDaoManager daoManager = DaoCommon.GetDaoManager();
- IAccountDao accountDao = (IAccountDao)daoManager[typeof(IAccountDao)];
- System.Collections.ArrayList List = accountDao.GetAllAccount();
其中***條語句是一個(gè)初始化的操作,它會(huì)根據(jù)配置文件,連接實(shí)際的數(shù)據(jù)庫:
- // Build the connection template
- type = assembly.GetType(_connectionClass, true);
- CheckPropertyType("DbConnectionClass", typeof(IDbConnection), type);
- _templateConnection = (IDbConnection)type.GetConstructor(Type.EmptyTypes).Invoke(null);
同時(shí)也會(huì)根據(jù)dao的配置初始化所有的dao代理放到靜態(tài)變量中,也就是以后的dao的調(diào)用都是從靜態(tài)變量中取出已經(jīng)初始化好的dao對(duì)象來進(jìn)行操作。注意:dao中變量會(huì)一直保存,被所有的用戶共享,所以要注意一些類變量的使用(和struts的action等類似)
第二條語句是從已經(jīng)處理好的靜態(tài)變量中取得一個(gè)指定的dao代理
第三條語句是調(diào)用實(shí)際的AccountDao中的GetAllAccount方法,在調(diào)用此方法前,會(huì)由攔截機(jī)攔截先進(jìn)入DaoProxy處理:
- DaoManager daoManager = _daoImplementation.DaoManager;
- if ( daoManager.IsDaoSessionStarted() )
- {
- try
- {
- result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments);
- }
- catch (Exception e)
- {
- throw UnWrapException(e, invocation.Method.Name);
- }
- }
- else
- {
- Logging#region Logging
- if (_logger.IsDebugEnabled)
- {
- _logger.Debug("Dao Proxy, Open a connection ");
- }
- #endregion
- // Open a connection
- try
- {
- daoManager.OpenConnection();
- result = invocation.Method.Invoke(_daoImplementation.DaoInstance, arguments);
- }
- catch (Exception e)
- {
- throw UnWrapException(e, invocation.Method.Name);
- }
- finally
- {
- daoManager.CloseConnection();
- }
- }
從上面代碼我們可以看出,它分成了兩種情況,一種是有事務(wù)的情況,一種是沒有事務(wù)的情況。由于在有事務(wù)情況時(shí):IDalSession session = daoManager.BeginTransaction();已經(jīng)創(chuàng)建了數(shù)據(jù)庫連接,所以在這里就不再創(chuàng)建了。對(duì)于沒有事務(wù)的情況,調(diào)用daoManager.OpenConnection();來創(chuàng)建數(shù)據(jù)庫連接,實(shí)際創(chuàng)建代碼如下:
- if (_templateConnectionIsICloneable)
- {
- return (IDbConnection) ((ICloneable)_templateConnection).Clone();
- }
- else
- {
- return (IDbConnection) Activator.CreateInstance(_templateConnection.GetType());
- }
這里使用了connection的Clone方法,也也就是通過初始化時(shí)的connection來克隆一個(gè)新的connection,不需要重新創(chuàng)建,創(chuàng)建好之后把它保存在HttpContext中:
- public override void Store(ISqlMapSession session)
- {
- HttpContext currentContext = ObtainSessionContext();
- currentContext.Items[sessionName] = session;
- }
HttpContext相當(dāng)于一個(gè)請(qǐng)求靜態(tài)變量,可以保存同一個(gè)請(qǐng)求的靜態(tài)信息,方便以后的使用。
***調(diào)用實(shí)際的AccountDao中的代碼:
- IDaoManager daoManager = DaoManager.GetInstance(this);
- SqlMapDaoSession daoSession = daoManager.LocalDaoSession as SqlMapDaoSession;
- _sqlMap = daoSession.SqlMap;
- ArrayList list = (ArrayList)sqlMap.QueryForList("GetAllAccountsViaResultMap", null);
- return list;
這里就是從HttpContext中取得連接進(jìn)行數(shù)據(jù)庫處理的。
以上只有AccountBusiness和AccountDao中的代碼需要我們自己寫的,其他的都是框架中摘抄的重點(diǎn)代碼
以上就是一個(gè)常規(guī)的iBATIS.NET連接數(shù)據(jù)庫查詢操作,通過以上分析我們發(fā)現(xiàn)可以用以下語句來直接進(jìn)行數(shù)據(jù)庫操作(不需要dao,不需要dao的配置、跳過攔截機(jī)的處理-----這樣可以提高一點(diǎn)性能,但同時(shí)會(huì)增加代碼的復(fù)雜性,打破分層編碼的好處):
- IDaoManager daoManager = DaoCommon.GetDaoManager();
- daoManager.OpenConnection();
- SqlMapDaoSession daoSession = daoManager.LocalDaoSession as SqlMapDaoSession;
- ArrayList List = (ArrayList)daoSession.SqlMap.QueryForList("GetAllAccountsViaResultMap", null);
- daoManager.CloseConnection();
iBATIS.NET連接數(shù)據(jù)庫的情況就向你介紹到這里,希望通過本文的iBATIS.NET連接數(shù)據(jù)庫的講解對(duì)你的理解有所幫助。
【編輯推薦】