动态代理写connection连接池Demo

 1 public class JdbcUtil2 {
 2     //声明连接池<放到LinkedList中,操作其中对象的速度快 只需要改变连接>
 3     private static LinkedList<Connection> connectionspool=new LinkedList<Connection>();
 4     //静态代码块
 5     static{            
 6         try {
 7             String url="jdbc:mysql://localhost:3306/jdbcdb";
 8             String user="root";
 9             String password="mysql";
10             Class.forName("com.mysql.jdbc.Driver");
11             //创建3个连接并将它们代理
12             for(int i=0;i<3;i++)
13             {
14                 final Connection conn=DriverManager.getConnection(url, user, password);
15                 //对conn进行代理
16                 Object proxyobj= Proxy.newProxyInstance(
17                         JdbcUtil2.class.getClassLoader(), 
18                         new Class[]{Connection.class},
19                         new InvocationHandler() {                            
20                             public Object invoke(Object proxy, Method method, Object[] args)
21                                     throws Throwable {
22                                     //是否是close方法
23                                     if(method.getName().equals("close"))
24                                     {
25                                         synchronized(connectionspool){
26                                             connectionspool.addLast((Connection)proxy);
27                                             connectionspool.notify();
28                                         }
29                                         return null;//如果调用的是close()方法,不会调用代理类的方法,会调用代理
30                                     }
31                                     return method.invoke(conn, args);
32                             }
33                         });
34                 
35                 connectionspool.add((Connection)proxyobj);
36                 
37             }
38         } catch (SQLException e) {
39             // TODO Auto-generated catch block
40             e.printStackTrace();
41         } catch (ClassNotFoundException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }        
45     }
46     
47     
48     public static Connection getConnection()
49     {
50         synchronized(connectionspool)
51         {
52             if(connectionspool.size()==0)
53             {
54                 try {
55                     connectionspool.wait();
56                 } catch (InterruptedException e) {
57                     // TODO Auto-generated catch block
58                     e.printStackTrace();
59                 }
60                 return getConnection();
61             }
62             else {
63                 Connection conn=connectionspool.removeFirst();
64                 System.err.println("pool中还有连接数:"+connectionspool.size());
65                 return conn;
66             }
67         }
68     }    
69 }

利用多线程测试代理连接池

 1 public class TestProxy  {
 2     public static void main(String[] args) {
 3         for(int i=0;i<3000;i++)
 4         {
 5             new MyThread().start();
 6         }
 7     }
 8 }
 9 
10 class MyThread extends Thread
11 {
12     @Override
13     public void run() {
14         Connection con = null;
15         try{
16             con = JdbcUtil2.getConnection();
17             System.err.println(this.getName()+","+con);
18             con.setAutoCommit(false);//设置事务的开始
19             String sql = "insert into users values('"+this.getName()+"','Tom','44')";
20             Statement st = con.createStatement();
21             st.execute(sql);
22             con.commit();
23             System.err.println(this.getName()+"子线程执行完成。。。。。");
24         }catch(Exception e){
25             e.printStackTrace();
26         }finally{
27             try {
28                 con.setAutoCommit(true);
29                 con.close();
30             } catch (SQLException e) {
31                 e.printStackTrace();
32             }
33         }
34     }
35 }
View Code

 

转载于:https://www.cnblogs.com/liuwt365/p/4101514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Spring Boot连接池配置: 1. 添加依赖 在 `pom.xml` 文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.1</version> </dependency> </dependencies> ``` 其中,`spring-boot-starter-jdbc` 是 Spring Boot JDBC 的启动器,`HikariCP` 是一个高性能的 JDBC 连接池。 2. 配置数据源 在 `application.properties` 文件中添加以下配置: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.hikari.connection-timeout=20000 spring.datasource.hikari.maximum-pool-size=10 ``` 其中,`spring.datasource.url`、`spring.datasource.username`、`spring.datasource.password`、`spring.datasource.driver-class-name` 分别是数据库连接信息,`spring.datasource.hikari.connection-timeout` 是连接超时时间,`spring.datasource.hikari.maximum-pool-size` 是最大连接数。 3. 使用连接池 在需要使用连接池的代码中注入 `DataSource` 数据源即可使用连接池。例如: ```java @RestController public class UserController { @Autowired private DataSource dataSource; @GetMapping("/users") public List<User> getUsers() throws SQLException { try (Connection connection = dataSource.getConnection()) { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); List<User> users = new ArrayList<>(); while (resultSet.next()) { User user = new User(); user.setId(resultSet.getInt("id")); user.setName(resultSet.getString("name")); user.setAge(resultSet.getInt("age")); users.add(user); } return users; } } } ``` 以上就是一个简单的 Spring Boot 连接池配置和使用示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值