2014-03-03 52 views
2

在我的应用程序中,当我需要在这些方法中访问我的数据库时,我一直将我的SessionFatory传递给我的方法参数。它使用实例在我的控制器:何时创建新的SessionFactory?

@Autowired 
private SessionFactory sessionFactory; 

当我需要访问我的数据库,我用线这样的:

sessionFactory.getCurrentSession().save() 
// or 
List products = sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM PRODUCTS").list(); 

我应该创造新的sessionFactories让我当前的会话,或者应该是它可以将它作为参数传递吗?

编辑[添加为了清楚]:

从HomeController.java:

@Controller 公共类的HomeController {

@Autowired 
private SessionFactory sessionFactory; 
//Correlations Insert Handler 
//DOCUMENT CREATE 
@RequestMapping(value="/documents", method = RequestMethod.PUT) 
public @ResponseBody String insertDocument(HttpServletRequest request, HttpServletResponse response){  

    DocumentControl documentControl = new DocumentControl(); 
    documentControl.insertDocument(request, response, sessionFactory); 
    return "went through just find"; 

} 
//DOCUMENT READ 
@RequestMapping(value="/documents", method = RequestMethod.GET) 
public @ResponseBody List<Documents> getAllDocuments(){ 
    //List documents = sessionFactory.getCurrentSession().createQuery("from Documents").list();   
    DocumentControl documentControl = new DocumentControl(); 
    List documents = documentControl.getAllDocuments(sessionFactory); 
    return documents; 
} 

从DocumentControl.java:

public class DocumentControl { 

     private static Logger logger = Logger.getLogger(DocumentControl.class.getName()); 
     public DocumentControl(){}; 
     //CREATE 
     public void insertDocument(HttpServletRequest request, HttpServletResponse response, SessionFactory sessionFactory){ 
    Documents document = new Documents(request) 
     sessionFactory.getCurrentSession().save(document); 
     }   

     //READ 
     public List<DocumentReturn> getAllDocuments(SessionFactory sessionFactory){ 
      List documents = sessionFactory.getCurrentSession().createQuery("from Documents").list(); 

      return documents; 
     } 



     private boolean ifProductExists (String productCode, SessionFactory sessionFactory) { 
      boolean exists = false; 

      List<Products> productList = sessionFactory.getCurrentSession().createCriteria(Products.class).add(Restrictions.eq("productCode", productCode)).list(); 

      if (!productList.isEmpty() && productList.size() > 0) { 
       exists = true; 
      } 

      return exists; 
     } 

     private boolean ifStandardExists (String standardCode, SessionFactory sessionFactory) { 
      boolean exists = false; 

      List<Standards> standardList = sessionFactory.getCurrentSession().createCriteria(Standards.class).add(Restrictions.eq("standardCode", standardCode)).list(); 

      if (!standardList.isEmpty() && standardList.size() > 0) { 
       exists = true; 
      } 

      return exists; 
     } 
    } 

hibernate.cfg.xml:

hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.c3p0.acquire_increment">1</property> 
     <property name="hibernate.c3p0.idle_test_period">100</property> 
     <property name="hibernate.c3p0.max_size">10</property> 
     <property name="hibernate.c3p0.max_statements">10</property> 
     <property name="hibernate.c3p0.min_size">10</property> 
     <property name="hibernate.c3p0.timeout">100</property>  
     <mapping class="***.*****.********.model.Documents"/> 
    </session-factory> 
</hibernate-configuration> 

持久性的context.xml:

<bean id="dataSource" 
     class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" />  
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}" /> 
    </bean> 
    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>    
       <prop key="hibernate.show_sql">true</prop>    
       <prop key=" hibernate.use_sql_comments">true</prop>  

      </props> 
     </property> 
    </bean> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     < 

property name="sessionFactory" ref="sessionFactory" /> 
</bean> 
+0

我没有看到您创建新的'SessionFactory'对象。 –

+0

为什么你需要2个'SessionFactory'实例?请记住,'SessionFactory'实例是昂贵的资源。当你真的需要时,你应该只创建一个额外的对象,例如,当你想连接到单个应用程序中的多个数据库时。 –

回答

0
的SessionFactory

Singleton。因此,您应该从ApplicationContext获取它(即使用@Autowired注释)。将它作为参数传递只会使API复杂化。

+0

这个主题与'Singleton Pattern'没什么关系,主要关注主要问题。 – Antoniossss

+0

是的。你认为可以传递一个Singleton作为参数吗? – Andres

+0

我认为这是一个很好的话题。 OP是自动装配它的,所以就这方面而言,自动装配就很好。 – Antoniossss

0

我认为您应该将SessionFactory的创建权保留到您正在使用的框架中,并将其作为资源注入,就像您现在所做的那样。除此之外你根本没有在代码中创建它。好的做法是为每一块数据操作创建单独的会话,如果它不是由框架的c完成。会议应该是你的主要工作单位。

+0

所以如果我需要访问另一个班级的数据库,我只需要创建一个新的Session。那么session.getCurrentSession()? – Jonathan

+0

当您调用getCurrentSession()方法时,您不会创建会话。 – Andres

+0

@Andres你是对的,但只是部分因为它取决于实现(会话可以绑定到线程并被创建为请求)以及它用于的上下文。 – Antoniossss

相关问题