Spring Boot 配置数据库连接池,但日志却每次都新建连接的解决办法
By:Roy.LiuLast updated:2018-07-19
用spring boot 搭建了项目,配置了数据库连接池方面的东西。
但项目跑起来之后,总是报这样的日志:JDBC Connection [xxx] will not be managed by Spring,
Creating a new SqlSession
...
Closing non transactional SqlSession
完整日志如下:
这样的话,完全就没有用到数据库连接池了,性能也不好。
其实解决的方法,很简单粗暴:
在每个Service上加上@Transactional 的注解,就可以
但项目跑起来之后,总是报这样的日志: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 的注解,就可以
From:一号门
Previous:网聊趣图
COMMENTS