欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
關(guān)于OpenSessionInViewFilter

  
 

 

關(guān)于OpenSessionInViewFilter

轉自:Potain 的BLOG

OpenSessionInView

Created by potian. Last edited by admin 61 days ago. Viewed 181 times.
[edit] [attach]
Hibernate的Lazy初始化1:n關(guān)系時(shí),你必須保證是在同一個(gè)Session內部使用這個(gè)關(guān)系集合,不然Hiernate將拋出例外。

另外,你不愿意你的DAO測試代碼每次都打開(kāi)關(guān)系Session,因此,我們一般會(huì )采用OpenSessionInView模式。

OpenSessionInViewFilter解決Web應用程序的問(wèn)題

如果程序是在正常的Web程序中運行,那么Spring的OpenSessionInViewFilter能夠解決問(wèn)題,它:

protected void doFilterInternal(HttpServletRequest request,                HttpServletResponse response,                           FilterChain filterChain) throws ServletException, IOException {                      SessionFactory sessionFactory = lookupSessionFactory();                      logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");                      Session session = getSession(sessionFactory);                      TransactionSynchronizationManager.bindResource(sessionFactory,                new SessionHolder(session));                      try {                            filterChain.doFilter(request, response);                      }                      finally {                            TransactionSynchronizationManager.unbindResource(sessionFactory);                            logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");                            closeSession(session, sessionFactory);                      }                }
可以看到,這個(gè)Filter在request開(kāi)始之前,把sessionFactory綁定到TransactionSynchronizationManager,和這個(gè)SessionHolder相關(guān)。這個(gè)意味著(zhù)所有request執行過(guò)程中將使用這個(gè)session。而在請求結束后,將和這個(gè)sessionFactory對應的session解綁,并且關(guān)閉Session。

為什么綁定以后,就可以防止每次不會(huì )新開(kāi)一個(gè)Session呢?看看HibernateDaoSupport的情況:

public final void setSessionFactory(SessionFactory sessionFactory) {                this.hibernateTemplate = new HibernateTemplate(sessionFactory);                }                protected final HibernateTemplate getHibernateTemplate() {                return hibernateTemplate;                }

我們的DAO將使用這個(gè)template進(jìn)行操作:

public abstract class BaseHibernateObjectDao                      extends HibernateDaoSupport                      implements BaseObjectDao {                

      protected BaseEntityObject getByClassId(final long id) {             BaseEntityObject obj =                   (BaseEntityObject) getHibernateTemplate()                         .execute(new HibernateCallback() {

                  public Object doInHibernate(Session session)                         throws HibernateException {                         return session.get(getPersistentClass(), new Long(id));                   }

            });             return obj;       }

      public void save(BaseEntityObject entity) {             getHibernateTemplate().saveOrUpdate(entity);       }

      public void remove(BaseEntityObject entity) {             try {

                  getHibernateTemplate().delete(entity);             } catch (Exception e) {                   throw new FlexEnterpriseDataAccessException(e);             }       }

      public void refresh(final BaseEntityObject entity) {             getHibernateTemplate().execute(new HibernateCallback() {

                  public Object doInHibernate(Session session)                         throws HibernateException {                         session.refresh(entity);                         return null;                   }

            });       }

      public void replicate(final Object entity) {             getHibernateTemplate().execute(new HibernateCallback() {

                  public Object doInHibernate(Session session)                         throws HibernateException {                         session.replicate(entity, ReplicationMode.OVERWRITE);                         return null;                   }

            });       }

而HibernateTemplate試圖每次在execute之前去獲得Session,執行完就力爭關(guān)閉Session
public Object execute(HibernateCallback action) throws DataAccessException {                      Session session = (!this.allowCreate ?                            SessionFactoryUtils.getSession(getSessionFactory(),                false) :                            SessionFactoryUtils.getSession(getSessionFactory(),                getEntityInterceptor(),                getJdbcExceptionTranslator()));                      boolean existingTransaction =                TransactionSynchronizationManager.hasResource(getSessionFactory());                      if (!existingTransaction && getFlushMode() == FLUSH_NEVER) {                            session.setFlushMode(FlushMode.NEVER);                      }                      try {                            Object result = action.doInHibernate(session);                            flushIfNecessary(session, existingTransaction);                            return result;                      }                      catch (HibernateException ex) {                            throw convertHibernateAccessException(ex);                      }                      catch (SQLException ex) {                            throw convertJdbcAccessException(ex);                      }                      catch (RuntimeException ex) {                            // callback code threw application exception                            throw ex;                      }                      finally {                            SessionFactoryUtils.closeSessionIfNecessary(                session, getSessionFactory());                      }                }
而這個(gè)SessionFactoryUtils能否得到當前的session以及closeSessionIfNecessary是否真正關(guān)閉session,端取決于這個(gè)session是否用sessionHolder和這個(gè)sessionFactory在我們最開(kāi)始提到的TransactionSynchronizationManager綁定。
public static void closeSessionIfNecessary(Session session,                SessionFactory sessionFactory)                throws CleanupFailureDataAccessException {                      if (session == null ||                         TransactionSynchronizationManager.hasResource(sessionFactory)) {                            return;                      }                      logger.debug("Closing Hibernate session");                      try {                            session.close();                      }                      catch (JDBCException ex) {                            // SQLException underneath                            throw new CleanupFailureDataAccessException(                            "Cannot close Hibernate session", ex.getSQLException());                      }                      catch (HibernateException ex) {                            throw new CleanupFailureDataAccessException(                            "Cannot close Hibernate session", ex);                      }                }

HibernateInterceptor和OpenSessionInViewInterceptor的問(wèn)題

使用同樣的方法,這兩個(gè)Interceptor可以用來(lái)解決問(wèn)題。但是關(guān)鍵的不同之處在于,它們的力度只能定義在DAO或業(yè)務(wù)方法上,而不是在我們的Test方法上,除非我們把它們應用到TestCase的方法上,但你不大可能為T(mén)estCase去定義一個(gè)接口,然后把Interceptor應用到這個(gè)接口的某些方法上。直接使用HibernateTransactionManager也是一樣的。因此,如果我們有這樣的測試:

Category parentCategory  = new Category ();                      parentCategory.setName("parent");                      dao.save(parentCategory);                

      Category childCategory = new Category(); childCategory.setName("child");

      parentCategory.addChild(childCategory);       dao.save(childCategory);

      Category savedParent = dao.getCategory("parent");       Category savedChild = (Category ) savedParent.getChildren().get(0);       assertEquals(savedChild, childCategory);

將意味著(zhù)兩件事情:
  • 每次DAO執行都會(huì )啟動(dòng)一個(gè)session和關(guān)閉一個(gè)session
  • 如果我們定義了一個(gè)lazy的關(guān)系,那么最后的Category savedChild = (Category ) savedParent.getChildren().get(0);將會(huì )讓hibernate報錯。

解決方案

一種方法是對TestCase應用Interceptor或者TransactionManager,但這個(gè)恐怕會(huì )造成很多麻煩。除非是使用增強方式的AOP.我前期采用這種方法(Aspectwerkz),在Eclipse里面也跑得含好。

另一種方法是在TestCase的setup和teardown里面實(shí)現和Filter完全一樣的處理,其他的TestCase都從這個(gè)TestCase繼承,這種方法是我目前所使用的。

posted on 2004-11-18 15:11 云在青天水在瓶 閱讀(2530) 評論(1)  編輯 收藏 引用 收藏至365Key

評論

# re: 關(guān)于OpenSessionInViewFilter  回復   

轉自:Karl Baum‘s Weblog

Karl Baum‘s Weblog

All | General | Java


Thursday July 08, 2004
Lazy Initialization and the DAO pattern with Hibernate and Spring

Hibernate and Lazy Initialization

Hibernate object relational mapping offers both lazy and non-lazy modes of object initialization. Non-lazy initialization retrieves an object and all of its related objects at load time. This can result in hundreds if not thousands of select statements when retrieving one entity. The problem is compounded when bi-directional relationships are used, often causing entire databases to be loaded during the initial request. Of course one could tediously examine each object relationship and manually remove those most costly, but in the end, we may be losing the ease of use benefit sought in using the ORM tool.

The obvious solution is to employ the lazy loading mechanism provided by hibernate. This initialization strategy only loads an object‘s one-to-many and many-to-many relationships when these fields are accessed. The scenario is practically transparent to the developer and a minimum amount of database requests are made, resulting in major performance gains. One drawback to this technique is that lazy loading requires the Hibernate session to remain open while the data object is in use. This causes a major problem when trying to abstract the persistence layer via the Data Access Object pattern. In order to fully abstract the persistence mechanism, all database logic, including opening and closing sessions, must not be performed in the application layer. Most often, this logic is concealed behind the DAO implementation classes which implement interface stubs. The quick and dirty solution is to forget the DAO pattern and include database connection logic in the application layer. This works for small applications but in large systems this can prove to be a major design flaw, hindering application extensibility.

Being Lazy in the Web Layer

Fortunately for us, the Spring Framework has developed an out of box web solution for using the DAO pattern in combination with Hibernate lazy loading. For anyone not familiar with using the Spring Framework in combination with Hibernate, I will not go into the details here, but I encourage you to read Hibernate Data Access with the Spring Framework. In the case of a web application, Spring comes with both the OpenSessionInViewFilter and the OpenSessionInViewInterceptor. One can use either one interchangeably as both serve the same function. The only difference between the two is the interceptor runs within the Spring container and is configured within the web application context while the Filter runs in front of Spring and is configured within the web.xml. Regardless of which one is used, they both open the hibernate session during the request binding this session to the current thread. Once bound to the thread, the open hibernate session can transparently be used within the DAO implementation classes. The session will remain open for the view allowing lazy access the database value objects. Once the view logic is complete, the hibernate session is closed either in the Filter doFilter method or the Interceptor postHandle method. Below is an example of the configuration of each component:

Interceptor Configuration

<beans>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
<property name="mappings">
...
</bean>
...
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
</beans>
Filter Configuration

<web-app>
...
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>
...
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.spring</url-pattern>
</filter-mapping>
...
</web-app>
Implementing the Hibernate DAO‘s to use the open session is simple. In fact, if you are already using the Spring Framework to implement your Hibernate DAO‘s, most likely you will not have to change a thing. The DAO‘s must access Hibernate through the convenient HibernateTemplate utility, which makes database access a piece of cake. Below is an example DAO.

Example DAO

public class HibernateProductDAO extends HibernateDaoSupport implements ProductDAO {

public Product getProduct(Integer productId) {
return (Product)getHibernateTemplate().load(Product.class, productId);
}

public Integer saveProduct(Product product) {
return (Integer) getHibernateTemplate().save(product);
}

public void updateProduct(Product product) {
getHibernateTemplate().update(product);
}
}
Being Lazy in the Business Layer

Even outside the view, the Spring Framework makes it easy to use lazy load initialization, through the AOP interceptor HibernateInterceptor. The hibernate interceptor transparently intercepts calls to any business object configured in the Spring application context, opening a hibernate session before the call, and closing the session afterward. Let‘s run through a quick example. Suppose we have an interface BusinessObject:

public interface BusinessObject {
public void doSomethingThatInvolvesDaos();
}
The class BusinessObjectImpl implements BusinessObject:


public class BusinessObjectImpl implements BusinessObject {
public void doSomethingThatInvolvesDaos() {
// lots of logic that calls
// DAO classes Which access
// data objects lazily
}
}
Through some configurations in the Spring application context, we can instruct the HibernateInterceptor to intercept calls to the BusinessObjectImpl allowing it‘s methods to lazily access data objects. Take a look at the fragment below:

<beans>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl">
<property name="someDAO"><ref bean="someDAO"/></property>
</bean>
<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target"><ref bean="businessObjectTarget"/></property>
<property name="proxyInterfaces">
<value>com.acompany.BusinessObject</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
</beans>

When the businessObject bean is referenced, the HibernateInterceptor opens a hibernate session and passes the call onto the BusinessObjectImpl. When the BusinessObjectImpl has finished executing, the HibernateInterceptor transparently closes the session. The application code has no knowledge of any persistence logic, yet it is still able to lazily access data objects.

Being Lazy in your Unit Tests

Last but not least, we‘ll need the ability to test our lazy application from J-Unit. This is easily done by overriding the setUp and tearDown methods of the TestCase class. I prefer to keep this code in a convenient abstract TestCase class for all of my tests to extend.

public abstract class MyLazyTestCase extends TestCase {

private SessionFactory sessionFactory;
private Session session;

public void setUp() throws Exception {
super.setUp();
SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
session = SessionFactoryUtils.getSession(sessionFactory, true);
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));

}

protected Object getBean(String beanName) {
//Code to get objects from Spring application context
}

public void tearDown() throws Exception {
super.tearDown();
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
}
}

( Jul 08 2004, 09:39:55 AM EDT ) Permalink Comments [2]

Trackback URL: http://jroller.com/trackback/kbaum/Weblog/orm_lazy_initialization_with_dao
Comments:


A few things to keep in the back of your mind if you take this approach; 1. If any errors occur while attempting to lazy load relationships in the view (JSP) it would be hard to present a nice error to the user. 2. This would result in at least 2 hibernate sessions (db connections being open for any one request), so you might want to up the number of connections available. Cheers, Dan
Posted by Dan Washusen on July 08, 2004 at 09:02 PM EDT #

I am a little confused on why it would be difficult to show a nice error jsp. Couldn‘t we just use the provided servlet container error page mechanisms? In regards to the 2 hibernate sessions being opened. Are you saying that the OpenSessionInViewInterceptor would be run twice if an exception was thrown? Thanks for your feedback!
Posted by Karl Baum (63.170.158.133) on July 09, 2004 at 09:48 AM EDT #
2004-11-18 15:16 | 逃離開(kāi)發(fā) 
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
OpenSessionInViewFilter源碼分析
hibernate exception 報錯 異?!局攸c(diǎn)】【總結】
Spring?的OpenSessionInViewFilter簡(jiǎn)介
spring+hibernate多數據源+動(dòng)態(tài)切換 事宜 lazyload一應俱全
struts2 hibernate spring配置管理(一)——配置文件 - Strut...
Spring整合Hibernate中對HibernateTemplate的思考
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久