我有简单的 Spring3 MVC 应用程序。我正在使用带有 MySQL 作为数据库和 Tomcat6 的 Hibernate 3。当我部署我的 war 文件时,第一次创建 SessionFactory 时,webapp 被初始化。但是tomcat进入无限循环并一次又一次地创建会话工厂。(即使没有从浏览器发送请求)我尝试了很多清理但不知道为什么会发生这种情况。请帮忙
因此,当我发送从 db 获取 domainList 的请求时,我的请求处于等待状态,因为 tomcat 再次创建 sessionfactory。因此,一旦创建它,就会获取数据并将其作为响应发送。
域DAO
public interface DomainDao {
public void addContact(Object contact);
public List<Object> listContact();
public void removeContact(Integer id);
}
域DAOImpl
@Repository
public class DomainDAOImpl implements DomainDao {
private SessionFactory sessionFactory;
@Autowired
public DomainDAOImpl(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public void addContact(Object contact) {
sessionFactory.getCurrentSession().save(contact);
}
@SuppressWarnings("unchecked")
public List<Object> listContact() {
return sessionFactory.getCurrentSession().createQuery("from Domain").list();
}
public void removeContact(Integer id) {
Domain domain = (Domain) sessionFactory.getCurrentSession().load(
Domain.class, id);
if (null != domain) {
sessionFactory.getCurrentSession().delete(domain);
}
}
}
域服务
public interface DomainService {
public void addContact(Object contact);
public List<Object> listContact();
public void removeContact(Integer id);
}
DomainServiceImpl
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dashboard.bo.DomainService;
import com.dashboard.dao.DomainDao;
import com.dashboard.entities.Domain;
@Service
public class DomainServiceImpl implements DomainService{
@Autowired
private DomainDao domainDAO;
@Transactional
public void addContact(Object domain) {
domainDAO.addContact(domain);
}
@Transactional
public List<Object> listContact() {
return domainDAO.listContact();
}
@Transactional
public void removeContact(Integer id) {
domainDAO.removeContact(id);
}
}
调度程序-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.dashboard.bo, com.dashboard.bo.impl, com.dashboard.controller, com.dashboard.dao, com.dashboard.dao.impl" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
version="2.5">
<display-name>HiberDash</display-name>
<description>HiberDash</description>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
</web-app>
Tomcat 控制台
> Jul 13, 2012 1:42:13 PM org.apache.catalina.core.ApplicationContext
> log INFO: Initializing Spring FrameworkServlet 'dispatcher' Jul 13,
> 2012 1:42:13 PM org.springframework.web.servlet.FrameworkServlet
> initServletBean INFO: FrameworkServlet 'dispatcher': initialization
> started Jul 13, 2012 1:42:13 PM
> org.springframework.context.support.AbstractApplicationContext
> prepareRefresh INFO: Refreshing WebApplicationContext for namespace
> 'dispatcher-servlet': startup date [Fri Jul 13 13:42:13 IST 2012];
> parent: Root WebApplicationContext Jul 13, 2012 1:42:13 PM
> org.springframework.beans.factory.xml.XmlBeanDefinitionReader
> loadBeanDefinitions INFO: Loading XML bean definitions from
> ServletContext resource [/WEB-INF/dispatcher-servlet.xml] Jul 13, 2012
> 1:42:14 PM org.springframework.core.io.support.PropertiesLoaderSupport
> loadProperties INFO: Loading properties file from ServletContext
> resource [/WEB-INF/jdbc.properties] Jul 13, 2012 1:42:14 PM
> org.springframework.beans.factory.support.DefaultListableBeanFactory
> preInstantiateSingletons INFO: Pre-instantiating singletons in
> org.springframework.beans.factory.support.DefaultListableBeanFactory@1357784:
> defining beans
> [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,domainServiceImpl,welcomeController,domainDAOImpl,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,messageSource,propertyConfigurer,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager];
> parent:
> org.springframework.beans.factory.support.DefaultListableBeanFactory@1bd06bf
> Jul 13, 2012 1:42:14 PM org.hibernate.cfg.Configuration configure
> INFO: configuring from url:
> jndi:/localhost/HiberDash/WEB-INF/hibernate.cfg.xml Jul 13, 2012
> 1:42:14 PM org.hibernate.cfg.Configuration doConfigure INFO:
> Configured SessionFactory: null Jul 13, 2012 1:42:14 PM
> org.hibernate.cfg.AnnotationBinder bindClass INFO: Binding entity from
> annotated class: com.dashboard.entities.Domain Jul 13, 2012 1:42:14 PM
> org.hibernate.cfg.annotations.EntityBinder bindTable INFO: Bind entity
> com.dashboard.entities.Domain on table domains Jul 13, 2012 1:42:14 PM
> org.springframework.orm.hibernate3.LocalSessionFactoryBean
> buildSessionFactory INFO: Building new Hibernate SessionFactory Jul
> 13, 2012 1:42:14 PM org.hibernate.connection.ConnectionProviderFactory
> newConnectionProvider INFO: Initializing connection provider:
> org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
> Jul 13, 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory
> buildSettings INFO: RDBMS: MySQL, version: 5.0.83-community-nt Jul 13,
> 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory buildSettings INFO:
> JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.7
> ( Revision: ${svn.Revision} ) Jul 13, 2012 1:42:23 PM
> org.hibernate.dialect.Dialect <init> INFO: Using dialect:
> org.hibernate.dialect.MySQLDialect Jul 13, 2012 1:42:23 PM
> org.hibernate.transaction.TransactionFactoryFactory
> buildTransactionFactory INFO: Transaction strategy:
> org.springframework.orm.hibernate3.SpringTransactionFactory Jul 13,
> 2012 1:42:23 PM
> org.hibernate.transaction.TransactionManagerLookupFactory
> getTransactionManagerLookup INFO: No TransactionManagerLookup
> configured (in JTA environment, use of read-write or transactional
> second-level cache is not recommended) Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic flush
> during beforeCompletion(): disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Automatic
> session close at end of transaction: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC batch size:
> 15 Jul 13, 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory
> buildSettings INFO: JDBC batch updates for versioned data: disabled
> Jul 13, 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory
> buildSettings INFO: Scrollable result sets: enabled Jul 13, 2012
> 1:42:23 PM org.hibernate.cfg.SettingsFactory buildSettings INFO: JDBC3
> getGeneratedKeys(): enabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Connection
> release mode: auto Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Maximum outer
> join fetch depth: 2 Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Default batch
> fetch size: 1 Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Generate SQL
> with comments: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL
> updates by primary key: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Order SQL
> inserts for batching: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory INFO:
> Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory Jul
> 13, 2012 1:42:23 PM org.hibernate.hql.ast.ASTQueryTranslatorFactory
> <init> INFO: Using ASTQueryTranslatorFactory Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Query language
> substitutions: {} Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: JPA-QL strict
> compliance: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Second-level
> cache: enabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Query cache:
> disabled Jul 13, 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory
> createCacheProvider INFO: Cache provider:
> org.hibernate.cache.NoCacheProvider Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Optimize cache
> for minimal puts: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Structured
> second-level cache entries: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Echoing all SQL
> to stdout Jul 13, 2012 1:42:23 PM org.hibernate.cfg.SettingsFactory
> buildSettings INFO: Statistics: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Deleted entity
> synthetic identifier rollback: disabled Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Default
> entity-mode: pojo Jul 13, 2012 1:42:23 PM
> org.hibernate.cfg.SettingsFactory buildSettings INFO: Named query
> checking : enabled Jul 13, 2012 1:42:23 PM
> org.hibernate.impl.SessionFactoryImpl <init> INFO: building session
> factory Jul 13, 2012 1:42:23 PM
> org.hibernate.impl.SessionFactoryObjectFactory addInstance INFO: Not
> binding factory to JNDI, no JNDI name configured Jul 13, 2012 1:42:23
> PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/welcome] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/welcome.*] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/welcome/] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Root mapping to handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getDomain] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getDomain.*] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getDomain/] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getUsers] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getUsers.*] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
> registerHandler INFO: Mapped URL path [/getUsers/] onto handler
> [com.dashboard.controller.WelcomeController@18aeabe] Jul 13, 2012
> 1:42:23 PM
> org.springframework.orm.hibernate3.HibernateTransactionManager
> afterPropertiesSet INFO: Using DataSource
> [org.apache.commons.dbcp.BasicDataSource@18bff68] of Hibernate
> SessionFactory for HibernateTransactionManager Jul 13, 2012 1:42:23 PM
> org.springframework.web.servlet.FrameworkServlet initServletBean INFO:
> FrameworkServlet 'dispatcher': initialization completed in 9360 ms