Hibernate操作資料庫的回調機制--Callback

來源:互聯網
上載者:User

1:一般情況下,在使用Hibernate Session存取資料庫的代碼中,基本上大部分是相同的,如下兩個方法所示,

 
  1. //查詢Teacher操作  
  2. ublic Teacher getTeacher(Long id) throws DataAccessException {  
  3. Session session = getSession();  
  4. Teacher teacher = null;  
  5. try {  
  6.     teacher = (Teacher)session.get(Teacher.class, id);  
  7.     ...  
  8.       
  9. } catch(HibernateException ex) {  
  10.     throw convertHibernateAccessException(ex);  
  11. } catch(SQLException ex) {  
  12.     throw convertJdbcAccessException(ex);  
  13. } catch(RuntimeException ex) {  
  14.     throw ex;  
  15. } finally {  
  16.     session.close();  
  17.     return teacher;  
  18. }  
 
  1. //查詢Class操作  
  2. ublic classInfo getTeacher(Long id) throws DataAccessException {  
  3. Session session = getSession();  
  4. ClassInfo classInfo = null;  
  5. try {  
  6.     classInfo = (ClassInfo)session.get(ClassInfo.class, id);  
  7.     ...  
  8.       
  9. } catch(HibernateException ex) {  
  10.     throw convertHibernateAccessException(ex);  
  11. } catch(SQLException ex) {  
  12.     throw convertJdbcAccessException(ex);  
  13. } catch(RuntimeException ex) {  
  14.     throw ex;  
  15. } finally {  
  16.     session.close();  
  17.     return classInfo;  
  18. }  
上面這兩種方法的做法是不對的,大量重複的代碼會導致後期維護困難,這裡可以考慮把具體的商務邏輯處理部分剝離出來,而只對公用的Session擷取及釋放

和異常處理部分進行封裝,形成一個公用的方法,如下面代碼所示,

 
  1. //封裝後公用方法  
  2. ublic Object process(HibernateCallback action) throws DataAccessException {  
  3. Session session = getSession();  
  4. Object obj = null;  
  5. try {  
  6.     obj = action.doInHibernate(session);  
  7. } catch(HibernateException ex) {  
  8.     throw convertHibernateAccessException(ex);  
  9. } catch(SQLException ex) {  
  10.     throw convertJdbcAccessException(ex);  
  11. } catch(RuntimeException ex) {  
  12.     throw ex;  
  13. }  

 

  1. //查詢Teacher操作  
  2. public Teacher getTeacher(final Long id) throws DataAccessException {  
  3.     return (Teacher)process(new HibernateCallback(){  
  4.         public Object doInHibernate(Session session) throws HibernateException, SQLException {  
  5.             Teacher teacher = (Teacher)session.get(Teacher.class, id);  
  6.             return teacher;  
  7.         }  
  8.     });  
  9. }  
回調,就是由被調用者調用由調用者提供的對象實現具體的業務操作。回調機制一般與模板方法結合起來作為一種模式使用,回調機制讓應用程式人員只關心商務邏輯實現,

而不用處理一些底層通用操作,從而可以實現通用操作和商務邏輯相分離。比如在Java資料庫操作中,使用回調機制,不用關心資料庫連接的開啟和關閉,只需要在Callback實現相關的資料存取即可,這樣可以從一定程度上保證資料庫連接總是被及時關閉。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.