Java连接池实现

编程入门 行业动态 更新时间:2024-10-14 22:17:01
本文介绍了Java连接池实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

public class ConnectionPool { private static List< DBConnection> pool = null; private static int available = 0; private ConnectionPool(){} public static DBConnection getConnection(){ if(pool == null){ pool = new ArrayList< DBConnection> ; for(int i = 0; i <3; i ++){ try { pool.add(new DBConnection()); 可用++; } catch(SQLException e){ e.printStackTrace(); } } } if(pool.size()> 0){ available--; return pool.remove(available); } else { return null; } } public static void returnConnection(DBConnection c){ pool.add(c); 可用++; } }

我只使用一个数组,请求连接池连接使用它,然后将其返回到连接池。

连接连接= ConnectionPool.getConnection ; connection.execute(insert into users values('+ user.getUsername()+','+ user.getPassword()+','+ user.getGroup() '+ banned +','+ broker + admin + sharehodler + company +')); ConnectionPool.returnConnection(connection); connection = null;

请与我联系。谢谢

解决方案

有一些要点会使此实现非常有问题。

  • 线程安全。如果几个线程与池一起工作怎么办?
  • 3个连接的静态最大池大小,也可以立即创建所有这些连接,无论是否需要。一个常见的策略是创建一堆连接,并在需要时创建一些连接,直到达到允许/配置的最大值。
  • 您只有静态方法。应该可以有几个池,这意味着您需要 ConnectionPool 的实例。
  • 没有办法传递host + dbname + user +
  • 您不处理断开的连接 - 如果现有的连接断开,您可能必须重新创建连接。
  • 使用配置值而不是静态值,请参阅第2点
  • 最后:确实,有趣的是自己写这个东西 - 但如果你需要一个项目池,选择一个现有的,如 c3p0 或 tomcat连接池。

我确定还有更多要指出的,但除非这些都是固定的,否则不能继续使用。

can you look at my connection pool if it is a possible way to implement it?

public class ConnectionPool { private static List<DBConnection> pool = null; private static int available = 0; private ConnectionPool() {} public static DBConnection getConnection() { if (pool == null) { pool = new ArrayList<DBConnection>(); for (int i = 0; i < 3; i++) { try { pool.add(new DBConnection()); available++; } catch (SQLException e) { e.printStackTrace(); } } } if (pool.size() > 0) { available--; return pool.remove(available); } else { return null; } } public static void returnConnection(DBConnection c) { pool.add(c); available++; } }

I'm using only one array and the client should ask the connection pool for a connection use it and then return it to the connection pool.

Connection connection = ConnectionPool.getConnection(); connection.execute("insert into users values('"+user.getUsername()+"', '"+user.getPassword()+"', '"+user.getGroup()+"', '"+banned+"', '"+broker+admin+sharehodler+company+"')"); ConnectionPool.returnConnection(connection); connection = null;

Please I need feedback on this implementation. Thank you

解决方案

There are some points that make this implementation very problematic.

  • Thread safety. What if several threads work with the pool? You are not locking the list on read/write access.
  • Static maximum pool size of 3 connections, also you immediately create all of them, whether they are needed or not. A common strategy is to create a bunch of connections and create some more when needed, until the allowed/configured maximum is reached.
  • You only have static methods. It should be possible to have several pools, meaning you need instances of ConnectionPool.
  • No way to pass host+dbname+user+password to the connections that are created.
  • You don't deal with 'broken' connections - you may have to re-create a connection if an existing one screwed up. This is far more relevant than I thought before I started using pools.
  • Use config values instead of static values, see point #2
  • Lastly: sure, it's interesting to write this stuff yourself - but if you need a pool for a project, pick an existing one, such as c3p0 or the tomcat connection pool.

I'm sure there's more to point out, but unless these are fixed, there's no use in continuing.

更多推荐

Java连接池实现

本文发布于:2023-11-25 23:38:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631805.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接池   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!