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

2024-02-21 19:18

本文主要是介绍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

这篇关于Tomcat刚刚启动完毕,数据库的连接数1的问题解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/732793

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

如何通过try-catch判断数据库唯一键字段是否重复

《如何通过try-catch判断数据库唯一键字段是否重复》在MyBatis+MySQL中,通过try-catch捕获唯一约束异常可避免重复数据查询,优点是减少数据库交互、提升并发安全,缺点是异常处理开... 目录1、原理2、怎么理解“异常走的是数据库错误路径,开销比普通逻辑分支稍高”?1. 普通逻辑分支 v

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp