大家!
我有生以来第一次尝试使用连接池。我想将其应用于Java + Tomcat + maven + Spring MVC + Spring Security + Hibernate上的项目。作为实现连接池的库,我决定使用c3p0库。
我将c3p0的依赖项添加到pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>first-rest-project</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>FirstRestProject</name>
<description>CRUD methods testing in the REST API app</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<springframework.version>5.1.7.RELEASE</springframework.version>
<springsecurity.version>5.1.5.RELEASE</springsecurity.version>
<hibernate.version>5.2.8.Final</hibernate.version>
<jackson.library>2.9.8</jackson.library>
</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate ORM -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate-c3p0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- Jackson API for JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.library}</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
我在文件db.properties中添加了c3p0的连接池设置:
# MySQL properties
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/service
mysql.user=root
mysql.password=****
# Hibernate properties
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
#C3P0 properties
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=2
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.idle_test_period=3000
hibernate.c3p0.max_statements=0
我认为这足以满足c3p0的连接池的工作。但是我不知道它是否有效。 当我运行应用程序(在IDE中)时,我看到服务器日志中有minPoolSize-> 1和maxPoolSize-> 2。但是如何在实践中进行测试?如果使用JDBC请求,则将尝试创建多个连接。但是我与ORM合作,换句话说,就是与会议合作。那里怎么办?我尝试了以下操作:我从3个不同的浏览器发送了3个GET请求(它们执行对数据库的访问),但是尽管maxPoolSize = 2,但所有3个都成功执行了。
我的DAO类的代码:
package com.example.dao;
import com.example.model.User;
import org.hibernate.*;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.criteria.*;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private SessionFactory sessionFactory;
@Override
public Integer save(User user) {
sessionFactory.getCurrentSession().save(user);
return user.getId();
}
@Override
public User get(Integer id) {
return sessionFactory.getCurrentSession().get(User.class, id);
}
@Override
public User get(String username) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from User u where u.username = :login");
query.setParameter("login", username);
return (User) query.getResultList().get(0);
}
@Override
public void update(Integer id, User user2) {
Session session = sessionFactory.getCurrentSession();
User user1 = session.byId(User.class).load(id);
user1.setUsername(user2.getUsername());
user1.setPassword(user2.getPassword());
user1.setId_role(user2.getId_role());
user1.setName(user2.getName());
user1.setSurname(user2.getSurname());
user1.setEmail(user2.getEmail());
user1.setBirth(user2.getBirth());
session.flush();
}
@Override
public void delete(Integer id) {
Session session = sessionFactory.getCurrentSession();
User user = session.byId(User.class).load(id);
session.delete(user);
}
@Override
public List<User> list() {
Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> root = cq.from(User.class);
cq.select(root);
Query<User> query = session.createQuery(cq);
return query.getResultList();
}
}
我的Hibernate配置类的代码:
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import static org.hibernate.cfg.AvailableSettings.*;
import java.util.Properties;
/*
Hibernate configuration class
*/
@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.example.dao"), @ComponentScan("com.example.service")})
public class HibernateConfig {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean getSessionFactory(){
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put(DRIVER, env.getProperty("mysql.driver"));
props.put(URL, env.getProperty("mysql.url"));
props.put(USER, env.getProperty("mysql.user"));
props.put(PASS, env.getProperty("mysql.password"));
// Setting Hibernate properties
props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
// Setting C3P0 properties
props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
props.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment"));
props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
props.put(C3P0_IDLE_TEST_PERIOD, env.getProperty("hibernate.c3p0.idle_test_period"));
props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("com.example.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager(){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
我是否可以使用连接池?我该如何检查?有什么想法吗?
在此先感谢您提供任何答案或链接。
答案 0 :(得分:0)
在MySQL中,您可以随时使用show processlist
来检查活动连接及其各种属性。