Spring 3 MVC查询在几次调用后没有执行

时间:2014-11-04 14:08:30

标签: hibernate spring-mvc spring-3

我正在制作一个Spring 3 MVC Hibernate Web应用程序。我面临的问题是应用程序正常工作但是当我调用6或7个数据库调用时出现问题。尝试通过执行少量查询来访问数据。执行前6到7个查询但在此之后。查询停止执行意味着所有应用程序都没有响应。非常奇怪的情况。我粘贴服务器xml的服务,实现控制器和dao请指导我在哪里我错了搜索但仍然没有解决方案

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/MyData/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Spring Database DataSource -->
    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url"
            value="jdbc:mysql://localhost:3306/health" />
        <beans:property name="username" value="***" />
        <beans:property name="password" value="***" />
    </beans:bean>

    <!-- Hibernate 3 Session Factory  -->

    <beans:bean id="enversListener" class="org.hibernate.envers.event.AuditEventListener" />

    <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="packagesToScan" value="com.***.model"/>
        <beans:property name="hibernateProperties">
            <beans:props>
                  <beans:prop key="dialect">org.hibernate.dialect.HSQLDialect</beans:prop>
                  <beans:prop key="hibernate.jdbc.batch_size">20</beans:prop>
                  <beans:prop key="show_sql">true</beans:prop>
                  <beans:prop key="hbm2ddl.auto">update</beans:prop>
            </beans:props>
        </beans:property>

        <beans:property name="eventListeners">
          <beans:map>
               <beans:entry key="post-insert"><beans:ref local="enversListener" /></beans:entry>
               <beans:entry key="post-update"><beans:ref local="enversListener" /></beans:entry>
               <beans:entry key="post-delete"><beans:ref local="enversListener" /></beans:entry>
               <beans:entry key="pre-collection-update"><beans:ref local="enversListener" /></beans:entry>
               <beans:entry key="pre-collection-remove"><beans:ref local="enversListener" /></beans:entry>
               <beans:entry key="post-collection-recreate"><beans:ref local="enversListener" /></beans:entry>
         </beans:map>
      </beans:property>

    </beans:bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

    <!-- Bean Defination -->
    <beans:bean id="userDao" class="com.***.DAO.UserDaoImpl"/>
    <beans:bean id="packDao" class="com.***.DAO.PackageDaoImpl"/>
    <beans:bean id="adminDao" class="com.***.DAO.AdminDaoImpl"/>
    <beans:bean id="branchDao" class="com.***.DAO.BranchDaoImpl"/>
    <beans:bean id="companyDao" class="com.***.DAO.CompanyDaoImpl"/>
    <beans:bean id="SubpackDao" class="com.***.DAO.SubpackageDaoImpl"/>

    <!-- Services Bean -->
    <beans:bean id="userService" class="com.***.services.UserServiceImpl"/>
    <beans:bean id="packageService" class="com.***.services.PackageServiceImpl"/>


</beans:beans>

1. Controller类,它接收请求并调用服务实现。

@Controller
    @RequestMapping(value="/PackageController")
    public class PackageController implements PackageService{

        private PackageService packService;

        @Autowired
        @Qualifier("packageService")
        public void setPackService(PackageService packService) {
            this.packService = packService;
        }

        @RequestMapping(value="/get_package_list",method=RequestMethod.POST,produces="application/json")
        public @ResponseBody String callgetPackageList(){
            System.out.println("Get the package list");
            List<Package> packageList = this.getPackageList();
            Iterator <Package> itTemp = packageList.iterator();
            JSONObject json=null;
            JSONArray json_data_array= new JSONArray();

            while(itTemp.hasNext())
            {
                Package packageTemp = itTemp.next();
                json = new JSONObject();
                try {
                    json.put("Id", packageTemp.getPackageId());
                    json.put("PackageName", packageTemp.getPackageName());
                    json_data_array.put(json);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            json = new JSONObject();
            try {
                json.put("Id", "custom");
                json.put("PackageName", "Customise Package");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            json_data_array.put(json);

            return json_data_array.toString();
        }
    @Override
        public List<Package> getPackageList() {
            // TODO Auto-generated method stub
            return packService.getPackageList();
        }
    }

3.调用dao方法并应用事务注释的服务实现。

public class PackageServiceImpl implements PackageService{

        private PackageDao packageDao;

        @Autowired
        @Qualifier("packDao")
        public void setPackageDao(PackageDao packageDao) {
            this.packageDao = packageDao;
        }

        @Override
        @Transactional
        public List<Package> getPackageList() {
            return packageDao.getPackageList();
        }
    }

4.编写实际查询以执行主要问题的Dao实现在于serviceImplementation和package我猜。

public class PackageDaoImpl implements PackageDao{

            private SessionFactory sessionFactory;
            private SubpackageDao subpack;

            @Autowired
            @Qualifier("sessionFactory")
            public void setSessionFactory(SessionFactory sessionFactory) {
                this.sessionFactory = sessionFactory;
            }

            @Override
            public List<Package> getPackageList() {

                Session session=sessionFactory.openSession();
                List<Package> packageListTemp = null;
                Criteria criteria = session.createCriteria(Package.class);
                criteria.setFetchMode("subpackages", FetchMode.SELECT);
                packageListTemp = criteria.list();
                return packageListTemp;
            }
        }

1 个答案:

答案 0 :(得分:2)

终于得到了答案,希望能帮助遇到与我同样问题的人 问题的根本原因是

Session session=sessionFactory.openSession();

根据与HibernateTransaction相关的帖子,当我们使用openSession而不是getCurrentSession时,会话在范围spring容器之外打开。结果会话未关闭,因此应用程序没有响应查询调用。