Spring 开机连接池理解
Spring boot connection pool understanding
在 Spring 引导 application.properties 文件中,我们有以下选项:
server.tomcat.max-threads = 100
server.tomcat.max-connections = 100
spring.datasource.tomcat.max-active = 100
spring.datasource.tomcat.max-idle = 30
这是我的存储库class
public interface UserRepository extends JpaRepository<Users,Integer>{}
这是服务class
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserById(Integer id){return userRepository.findOne(id)}
问题是,userRepository 如何创建与数据库的连接,以及它会使用我的应用程序属性文件中的连接池。我来自 JDBC 和休眠,在那里我使用 DataManager、DataSource、Connection classes 来使用连接池,但是在 spring 启动时我没有任何代码行 classes,一切正常
它像以前一样工作,但使用 Spring 引导,Spring 为您完成更多任务。
有或没有 Spring,DAO class as UserRepository
不直接操作数据源,也不直接创建 JDBC 连接。
这些由您正在使用的 EntityManagerFactory
实现操作。
使用 Spring-Hibernate,您仍然需要配置 EntityManagerFactory
.
现在使用 Spring 启动,您无需配置它。
为您完成。
Spring 引导的新功能是您现在还可以配置服务器数据源属性:
server.tomcat.max-threads = 100
server.tomcat.max-connections = 100
spring.datasource.tomcat.max-active = 100
spring.datasource.tomcat.max-idle = 30
因为 Tomcat 服务器可以由 Spring 引导应用程序本身启动。
This part of the Spring Boot documentation 给出数据源实现的优先顺序:
Production database connections can also be auto-configured using a
pooling DataSource. Here’s the algorithm for choosing a specific
implementation:
We prefer the Tomcat pooling DataSource for its performance and
concurrency, so if that is available we always choose it.
Otherwise, if HikariCP is available we will use it.
If neither the Tomcat pooling datasource nor HikariCP are available
and if Commons DBCP is available we will use it, but we don’t
recommend it in production and its support is deprecated.
Lastly, if Commons DBCP2 is available we will use it.
更新:
根据Spring Boot 2.x,HikariCP 是默认的连接池机制。
在 Spring 引导 application.properties 文件中,我们有以下选项:
server.tomcat.max-threads = 100
server.tomcat.max-connections = 100
spring.datasource.tomcat.max-active = 100
spring.datasource.tomcat.max-idle = 30
这是我的存储库class
public interface UserRepository extends JpaRepository<Users,Integer>{}
这是服务class
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserById(Integer id){return userRepository.findOne(id)}
问题是,userRepository 如何创建与数据库的连接,以及它会使用我的应用程序属性文件中的连接池。我来自 JDBC 和休眠,在那里我使用 DataManager、DataSource、Connection classes 来使用连接池,但是在 spring 启动时我没有任何代码行 classes,一切正常
它像以前一样工作,但使用 Spring 引导,Spring 为您完成更多任务。
有或没有 Spring,DAO class as UserRepository
不直接操作数据源,也不直接创建 JDBC 连接。
这些由您正在使用的 EntityManagerFactory
实现操作。
使用 Spring-Hibernate,您仍然需要配置 EntityManagerFactory
.
现在使用 Spring 启动,您无需配置它。
为您完成。
Spring 引导的新功能是您现在还可以配置服务器数据源属性:
server.tomcat.max-threads = 100
server.tomcat.max-connections = 100
spring.datasource.tomcat.max-active = 100
spring.datasource.tomcat.max-idle = 30
因为 Tomcat 服务器可以由 Spring 引导应用程序本身启动。
This part of the Spring Boot documentation 给出数据源实现的优先顺序:
Production database connections can also be auto-configured using a pooling DataSource. Here’s the algorithm for choosing a specific implementation:
We prefer the Tomcat pooling DataSource for its performance and concurrency, so if that is available we always choose it.
Otherwise, if HikariCP is available we will use it.
If neither the Tomcat pooling datasource nor HikariCP are available and if Commons DBCP is available we will use it, but we don’t recommend it in production and its support is deprecated.
Lastly, if Commons DBCP2 is available we will use it.
更新: 根据Spring Boot 2.x,HikariCP 是默认的连接池机制。