"Deal in sight" with Hibernate, Spring, Struts

From the Hibernate Reference Guide: “Use a single database transaction to serve a client request, starting and executing it when opening and closing a session”

Does Spring support this pattern? I used Spring transaction support with the @Transactional annotation and the Open Session in View template (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter), but transactions should be limited by service methods, so I get several transactions per view, not just one .

+3
source share
3 answers

You probably don't need the type of transaction in the form of a view. Usually what happens on request,

  • ,
  • . json .

, " ", , 3 , , - , .

, , , . , , .

, , , . " ". ,

Service1.op1()
Service2.op2()

.

AppFacade.doOp1andOp2()

op1 op2 .

- Spring Struts . , tx db , , tx. =. : http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html

10.5.2

, - .

+1

" " (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter), , , HTTP (, JSP) @Transactional , .

, :

  • JSP, " " - " ".
  • -, () .

, , .

0

, Hibernate OpenSessionInViewFilter/Interceptor..... ...

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

public class CustomHibernateSessionViewFilter extends   OpenSessionInViewFilter {

protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = super.getSession(sessionFactory);
    session.setFlushMode(FlushMode.COMMIT);
    return session;
}

protected void closeSession(Session session, SessionFactory factory) {
    session.flush();
    super.closeSession(session, factory);
}

}

web.xml( ): -

<filter>
    <filter-name>OSIVF Filter</filter-name>
    <filter-class>your.path.to.CustomHibernateSessionViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OSIVF Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

, . , , .

0

Source: https://habr.com/ru/post/1786917/


All Articles