Skip to content

21. 结论

我们已经探讨了Struts 2框架的基础知识,并展示了一个融合了多种技术的应用程序。初学者和中级水平的读者都能从本文中找到有助于学习和提升的内容。但仍需参考专业书籍。我再次推荐以下书籍:

Struts 2实战》(Struts 2 in Action),作者:Donald Brown、Chad Michael Davis 和 Scott Stanlick,Manning 出版社出版。

最后,我想重点谈谈在编写本文示例时遇到的一些问题。我之前已经提到过 Struts 2 的一些意外行为,特别是在输入数字和日期时。以下是我遇到的其他几个问题:

让我们从 Tomcat 服务器上卸载 [pam-01] 应用程序:

Image

随后 Apache 控制台将显示以下日志:

1
2
3
4
5
6
7
8
janv. 05, 2012 11:18:58 AM org.springframework.context.support.AbstractApplicationContext doClose
Infos: Closing org.springframework.web.context.support.XmlWebApplicationContext@57488b9c: display name [Root WebApplicationContext]; startup date [Thu Jan 05 11:18:45 CET 2012]; root of context hierarchy
janv. 05, 2012 11:18:58 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@78ae9d8d: defining beans [config,metier]; root of factory hierarchy
janv. 05, 2012 11:18:58 AM org.apache.tiles.access.TilesAccess setContainer
Infos: Removing TilesContext for context: org.apache.catalina.core.ApplicationContextFacade
janv. 05, 2012 11:19:00 AM org.apache.catalina.startup.HostConfig checkResources
Infos: Repli (undeploy) de l'application web ayant pour chemin de contexte /pam-01

卸载成功。

让我们以同样的方式卸载 [pam-02]。日志如下:

janv. 05, 2012 11:21:07 AM org.springframework.context.support.AbstractApplicationContext doClose
Infos: Closing org.springframework.web.context.support.XmlWebApplicationContext@5d9131fa: display name [Root WebApplicationContext]; startup date [Thu Jan 05 11:11:10 CET 2012]; root of context hierarchy
janv. 05, 2012 11:21:07 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
Infos: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b66d238: defining beans [config,metier,employeDao,indemniteDao,cotisationDao,entityManagerFactory,dataSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,txManager,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0]; root of factory hierarchy
janv. 05, 2012 11:21:07 AM org.springframework.orm.jpa.AbstractEntityManagerFactoryBean destroy
Infos: Closing JPA EntityManagerFactory for persistence unit 'pam-PU'
janv. 05, 2012 11:21:07 AM org.hibernate.impl.SessionFactoryImpl close
Infos: closing
janv. 05, 2012 11:21:07 AM org.apache.tiles.access.TilesAccess setContainer
Infos: Removing TilesContext for context: org.apache.catalina.core.ApplicationContextFacade
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
Grave: The web application [/pam-02] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
Grave: The web application [/pam-02] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.
janv. 05, 2012 11:21:07 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
Grave: The web application [/pam-02] appears to have started a thread named [MySQL Statement Cancellation Timer] but has failed to stop it. This is very likely to create a memory leak.
janv. 05, 2012 11:21:08 AM org.apache.catalina.startup.HostConfig checkResources
Infos: Repli (undeploy) de l'application web ayant pour chemin de contexte /pam-02

第 12、14、16 行:报告了严重错误。其中两条表明可能存在内存泄漏。事实上,[pam-02] 应用程序确实存在内存泄漏。过一段时间后,该应用程序将无法使用。此时您必须关闭 NetBeans 并重新部署该应用程序。

所有错误均涉及线程。而 [pam-02] 应用程序本身并不使用线程。因此,我们必须怀疑所使用的框架:Struts 2、Spring 2.5 和 Tiles。我们可以合理地排除 Tiles,因为它很可能不处理线程。这样就只剩下 Struts 和 Spring 了。

随后我们进行另一项实验:

我们使用 JSF 框架而非 Struts 2 来实现 [Web] 层。其他方面均保持不变。随后,我们收到了与使用 Struts 2 时相同的错误信息。

因此,罪魁祸首似乎是Spring,或是Spring在此处使用的dbcp连接池,又或者是我自己——如果我没有正确使用Spring的话。我可能忘记了某个用于清理已创建线程的Spring配置;我可能……因此,这个内存泄漏问题的解决方案仍有待寻找。

再做一次实验。NetBeans IDE 自带了 Tomcat 和 GlassFish 服务器。让我们尝试在 GlassFish 上运行本文档中创建的应用程序:

  • 在[2]中,[example-08]项目[1]的属性
  • 在 [3] 中,Web 服务器选择

以下是在浏览器中检索到的页面:

未能实现预期的可移植性。这里有两个错误:

  • 在[1]中,国际化功能未能正常工作
  • 在 [2] 中,发生了异常

对于本文档中开发的大多数应用程序,我们都会遇到这两个相同的错误。我尚未尝试解决这一新问题。