2019年10月8日 星期二

Hibernate Web JNDI+DataSource+Listener+Filter

1、更改Servers內的context.xml

 <Resource
  name="jdbc/Jason" type="javax.sql.DataSource"
  auth="Container" username="sa" password="passw0rd"
  driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url="jdbc:sqlserver://localhost:1433;databaseName=Jason">
 </Resource>

2、修改hibernate.cfg.xml

    <session-factory>
    <!--  
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.password">passw0rd</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Jason</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    -->
        <property name="hibernate.connection.datasource">java:comp/env/jdbc/Jason</property>
        <property name="hibernate.current_session_context_class">thread</property>
        
        <property name="hibernate.show_sql">true</property>
                
    </session-factory>

3、撰寫ServletContextListener.java

 package util;

 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;

 public class SessionFactoryListener implements ServletContextListener {
  @Override
  public void contextInitialized(ServletContextEvent sce) {
   HibernateUtil.getSessionFactory();
   System.out.println("Session Factory Created");
  }
 
  @Override
  public void contextDestroyed(ServletContextEvent sce) {
   HibernateUtil.closeSessionFactory();
   System.out.println("Session Factory Destroyed");
  }
 }

4、撰寫OpenSessionViewFilter.java

package util;

 import java.io.IOException;

 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;

 import org.hibernate.SessionFactory;

 public class OpenSessionViewFilter implements Filter {

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  
   SessionFactory sessionfactory = HibernateUtil.getSessionFactory();
  
   try {
    sessionfactory.getCurrentSession().beginTransaction();
    System.out.println("Transaction Begin");
   
    chain.doFilter(request, response);
   
    sessionfactory.getCurrentSession().getTransaction().commit();
    System.out.println("Transaction Commit");
   }catch(Exception e) {
    sessionfactory.getCurrentSession().getTransaction().rollback();
    System.out.println("Transaction Rollback");
    chain.doFilter(request, response);
   }finally {
    sessionfactory.getCurrentSession().close();
    System.out.println("Session Closed");
   }

  }

 }

5、WEB-INF/web.xml內註冊ServletContextListener

 <resource-ref>
  <description>HibernateSQLServerJDBCConnection</description>
  <res-ref-name>jdbc/Jason</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>

 <listener>
  <listener-class>util.SessionFactoryListener</listener-class>
 </listener>

 <filter>
  <filter-name>OpenSessionViewFilter</filter-name>
  <filter-class>util.OpenSessionViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>OpenSessionViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

沒有留言:

張貼留言

無暇的程式碼(Clean code)金句

The only valid measurement of code quality: WTFs/minute.