Spring Boot 配置数据库连接池,但日志却每次都新建连接的解决办法 - 一号门-程序员的工作,程序员的生活(java,python,delphi实战)

Spring Boot 配置数据库连接池,但日志却每次都新建连接的解决办法

用spring boot 搭建了项目,配置了数据库连接池方面的东西。


但项目跑起来之后,总是报这样的日志:JDBC Connection [xxx] will not be managed by Spring,
Creating a new SqlSession
...
Closing non transactional SqlSession
完整日志如下:
程序代码 程序代码

2018-07-19 16:41:52.646 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] ==> Parameters: 0(Integer), 14(Integer)
2018-07-19 16:41:52.647 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] <==      Total: 1
2018-07-19 16:41:52.647 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 191] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46578877]
2018-07-19 16:41:52.647 DEBUG [http-nio-80-exec-4] o.s.jdbc.datasource.DataSourceUtils [DataSourceUtils.java : 340] Returning JDBC Connection to DataSource
2018-07-19 16:41:52.647 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 97] Creating a new SqlSession
2018-07-19 16:41:52.648 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 148] SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2f40f842] was not registered for synchronization because synchronization is not active
2018-07-19 16:41:52.648 DEBUG [http-nio-80-exec-4] o.s.jdbc.datasource.DataSourceUtils [DataSourceUtils.java : 114] Fetching JDBC Connection from DataSource
2018-07-19 16:41:52.648 DEBUG [http-nio-80-exec-4] o.m.s.t.SpringManagedTransaction [SpringManagedTransaction.java : 87] JDBC Connection [HikariProxyConnection@1437518317 wrapping com.mysql.jdbc.JDBC4Connection@1ae5046e] will not be managed by Spring
2018-07-19 16:41:52.648 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] ==>  Preparing: select count(*) from zxbjizhang where unid=? and zid=?
2018-07-19 16:41:52.648 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] ==> Parameters: 0(Integer), 13(Integer)
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] <==      Total: 1
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 191] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2f40f842]
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] o.s.jdbc.datasource.DataSourceUtils [DataSourceUtils.java : 340] Returning JDBC Connection to DataSource
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 97] Creating a new SqlSession
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 148] SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5643e152] was not registered for synchronization because synchronization is not active
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] o.s.jdbc.datasource.DataSourceUtils [DataSourceUtils.java : 114] Fetching JDBC Connection from DataSource
2018-07-19 16:41:52.649 DEBUG [http-nio-80-exec-4] o.m.s.t.SpringManagedTransaction [SpringManagedTransaction.java : 87] JDBC Connection [HikariProxyConnection@2120847664 wrapping com.mysql.jdbc.JDBC4Connection@1ae5046e] will not be managed by Spring
2018-07-19 16:41:52.650 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] ==>  Preparing: select count(*) from zxbjizhang where unid=? and zid=?
2018-07-19 16:41:52.650 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] ==> Parameters: 0(Integer), 11(Integer)
2018-07-19 16:41:52.650 DEBUG [http-nio-80-exec-4] c.h.c.v.d.Z.getCountByUnidAndZid [BaseJdbcLogger.java : 159] <==      Total: 1
2018-07-19 16:41:52.651 DEBUG [http-nio-80-exec-4] org.mybatis.spring.SqlSessionUtils [SqlSessionUtils.java : 191] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5643e152]
2018-07-19 16:41:52.651 DEBUG [http-nio-80-exec-4] o.s.jdbc.datasource.DataSourceUtils [DataSourceUtils.java : 340] Returning JDBC Connection to DataSource
2018-07-19 16:41:52.652 DEBUG [http-nio-80-exec-4] o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor [AbstractMessageConverterMethodProcessor.java : 277] Written [com.henpi.common.vo.ResponseResult@3120a6f9] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@28c68d96]
2018-07-19 16:41:52.652 DEBUG [http-nio-80-exec-4] o.s.web.servlet.DispatcherServlet [DispatcherServlet.java : 1076] Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-07-19 16:41:52.652 DEBUG [http-nio-80-exec-4] o.s.web.servlet.DispatcherServlet [FrameworkServlet.java : 1000] Successfully completed request


这样的话,完全就没有用到数据库连接池了,性能也不好。
其实解决的方法,很简单粗暴:
在每个Service上加上@Transactional 的注解,就可以

除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.