Tomcat刚刚启动完毕,数据库的连接数>1的问题解决

这个问题只有在用到spring的时候才会出现。

 

刚开始,Tomcat刚刚启动完毕,我的数据库连接就达到了50多个,我很纳闷,按照常理,Tomcat刚刚启动完毕,应该只是加载了web.xml中的一个applicationContext.xml,应该只有一个连接才对,为什么会这么多,后来找到原因了,因为连接的数量正好等于我的service的数量+1,而我的每个service都继承了一个BaseService,而BaseService中有这么个成员变量

private ApplicationContext SJBUtil = new ClassPathXmlApplicationContext( "classpath:applicationContext.xml");

 

我们都知道tomcat在加载applicationContext.xml,会将里面的所有bean创建一个实例,所以每一个实例都会创建一个新的ApplicationContext,当然也会创建一个数据库连接,后来我将private ApplicationContext SJBUtil = new ClassPathXmlApplicationContext( "classpath:applicationContext.xml");改成了private static  ApplicationContext SJBUtil = new ClassPathXmlApplicationContext( "classpath:applicationContext.xml");,这时候Tomcat刚刚启动完毕时,连接数是两个,我依然觉得这样不好,最佳的办法应该是直接从加载的web.xml中获取applicationContext.xml,方法如下:

首先新建一个类SJBInit.java,内容如下:

public class SJBInit {
 /**
  *
系统应用spring环境
  */
 private static ApplicationContext ctx;

 /**
  *
单实例对象
  */
 private static SJBInit instance = null;

    Vector temp = new Vector(50);

    /**
  *
构造函数
  */
 public SJBInit() {
  if (instance == null){
   instance = this;
  }
 }

 /**
  *
获得单实例对象
  *
  * @return
  */
 public static SJBInit getInstance() {
  if (instance == null)
   new SJBInit();
  return instance;
 }

 /**
  *
初始化Spring组件
  */
 public void init(Properties props) throws Exception {

  loadContextXML(props);

 }

 /**
  *
加载spring对象
  *
  * @param props
  */
 private void loadContextXML(Properties props) throws Exception{
  String path ="";
  /*LogFactory.getInstance().logRun(RunPriority.INFORMATIONAL,
    LogConstants.sysLogConstants.INT_SPRING_START,
    null
    );*/
  try {
   ServletContext servletContext = (ServletContext) props
     .get("APP_CONTEXT");
            if (servletContext != null)
    ctx = WebApplicationContextUtils
      .getRequiredWebApplicationContext(servletContext);
  }
 
  catch (Exception e) {
            e.printStackTrace();


        }
  if ((ctx == null) || (ctx.getBeanDefinitionNames().length == 0)) {

  }
 
 }

 /**
  *
得到spring的所有配置文件
  *
  * @param path
  * @return
  */
 private void setConfigFiles(String path) {

  File file = new File(path);
  if (file.isDirectory()) {
   File[] files = file.listFiles();
   for (int index = 0; index < files.length; index++) {
    String filePath = files[index].getPath();
    if (filePath.endsWith(".xml")) {
     this.temp.add(filePath);
    }
   }
  }
 }

 /**
  *
得到一个spring的配置对象
  *
  * @param name
  * @return
  */
 public Object getBean(String name) {
  if (ctx == null)
   return null;
  else
   return ctx.getBean(name);
 }

 
 /**
  *
获取单个信息
  *
  * @param key
  * @param object
  * @param request
  * @return
  */
 public static String getMessage(String key, Object[] object, Locale locale) {
  return ctx.getMessage(key, object, locale);
 }

}

然后创建InitServlet.java,内容如下:

public class InitServlet extends HttpServlet{
 
 static final long serialVersionUID = -1111516993124229949L;
 
 /**
  *
启动对象实例
  */
 private SJBInit sjbinit = SJBInit.getInstance();

 /**
  * servlet
初始化
  */
 public void init(ServletConfig config) throws ServletException {
 
  super.init(config);
  Properties props = new Properties();
  props.put("APP_CONTEXT", config.getServletContext());
  //
文件路径
  String prefix = getServletContext().getRealPath("/");
 
  // web
应用路径
  props.put("APP_PATH", prefix);
 
  try {
  
  sjbinit.init(props);
 
  }catch(Exception e){
  
  }
 }
}

然后在web.xml中配置

...

<servlet>
  <description>System init when start</description>
  <display-name>InitServlet</display-name>
  <servlet-name>InitServlet</servlet-name>
  <servlet-class>com.fone.platform.core.InitServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath:applicationContext.xml,
   classpath:applicationContext-*.xml
  </param-value>
 </context-param>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

.....

然后再创建类SJBUtil.java,内容如下:

public class SJBUtil {
 /**
  * sjb
管理类实例
  */
 private static SJBInit sjb = SJBInit.getInstance();

   
 /**
  *
得到一个系统配置 bean
  *
  * @param name bean
的配置名称
  * @return
如果系统没有加载返回 null
  */
 public static Object getBean(String name) {
  return sjb.getBean(name);
 }

}

然后在service类中通过SJBUtil.getBean("...");就可以获得spring中的dao类了

public UsersDAO getUsersDAO() {
  return (UsersDAO) SJBUtil.getBean(SJBNameConstants.DAO_USERS_BEAN_NAME);
 }

问题解决,最后Tomcat刚刚启动完毕,数据库连接数就是1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值