我遇到一个问题,我们有一组说数据库连接,多个用户同时请求它们。
我必须能够从现有连接池中为它们提供Conn对象,一旦完成使用它,就将它放回到可用连接池中。
我能想到的方法是为每个已使用和未使用的连接设置2套,并保持来回移动物体。
我不知道这是最好的方法。
任何人都可以提出更好的方法吗? 我想知道我们是否可以将连接obj标记为正在使用或者某种东西,以便我们可以取消这两组?
答案 0 :(得分:1)
有数以千计的实施可供选择:
这些池经过测试,正常工作,优化代码。不是你的自制解决方案。
您遇到的问题只是在编写弹性连接池的过程中遇到的问题。你提出的解决方案充其量是天真的。还有许多其他问题,从滥用池到连接丢失,你必须处理。不仅如此,代码必须是线程安全的。
对于一般稀缺资源池而不是JDBC连接,请使用Commons Pool。再次,这是经过测试的工作代码。
答案 1 :(得分:0)
不要重新发明轮子。现有解决方案,其中一个是Apache Commons池:https://commons.apache.org/proper/commons-pool/。您所要做的就是提供一个工厂来初始化您的连接,然后Apache将为您提供GenericObjectPool
(请参阅doc),您可以在访问borrowObject
之前致电returnObject
池中的连接,然后使用2016-02-27 15:59:57,956 INFO org.apache.hadoop.mapred.TaskTracker: Trying to launch : attempt_201602271541_0001_m_000001_0
2016-02-27 15:59:57,956 INFO org.apache.hadoop.mapred.TaskTracker: In TaskLauncher, current free slots : 2 and trying to launch attempt_201602$
2016-02-27 15:59:58,194 INFO org.apache.hadoop.mapred.JvmManager: In JvmRunner constructed JVM ID: jvm_201602271541_0001_m_189940062
2016-02-27 15:59:58,194 INFO org.apache.hadoop.mapred.JvmManager: JVM Runner jvm_201602271541_0001_m_189940062 spawned.
2016-02-27 15:59:58,555 INFO org.apache.hadoop.mapred.TaskTracker: JVM with ID: jvm_201602271541_0001_m_189940062 given task: attempt_20160227$
2016-02-27 15:59:59,261 INFO org.apache.hadoop.mapred.TaskTracker: attempt_201602271541_0001_m_000001_0 0.0%
2016-02-27 15:59:59,262 INFO org.apache.hadoop.mapred.TaskTracker: Task attempt_201602271541_0001_m_000001_0 is done.
2016-02-27 15:59:59,262 INFO org.apache.hadoop.mapred.TaskTracker: reported output size for attempt_201602271541_0001_m_000001_0 was 0
2016-02-27 15:59:59,263 INFO org.apache.hadoop.mapred.TaskTracker: addFreeSlot : current free slots : 2
2016-02-27 15:59:59,309 INFO org.apache.hadoop.mapred.JvmManager: JVM : jvm_201602271541_0001_m_189940062 exited. Number of tasks it ran: 1
2016-02-27 16:00:00,974 INFO org.apache.hadoop.mapred.TaskTracker: LaunchTaskAction (registerTask): attempt_201602271541_0001_r_000000_0 task'$
2016-02-27 16:00:00,975 INFO org.apache.hadoop.mapred.TaskTracker: Trying to launch : attempt_201602271541_0001_r_000000_0
2016-02-27 16:00:00,975 INFO org.apache.hadoop.mapred.TaskTracker: In TaskLauncher, current free slots : 2 and trying to launch attempt_201602$
2016-02-27 16:00:01,016 INFO org.apache.hadoop.mapred.JvmManager: In JvmRunner constructed JVM ID: jvm_201602271541_0001_r_189940062
2016-02-27 16:00:01,017 INFO org.apache.hadoop.mapred.JvmManager: JVM Runner jvm_201602271541_0001_r_189940062 spawned.
2016-02-27 16:00:01,374 INFO org.apache.hadoop.mapred.TaskTracker: JVM with ID: jvm_201602271541_0001_r_189940062 given task: attempt_20160227$
2016-02-27 16:00:06,662 INFO org.apache.hadoop.mapred.TaskTracker: Sent out 6 bytes for reduce: 0 from map: attempt_201602271541_0001_m_000001$
2016-02-27 16:00:06,663 INFO org.apache.hadoop.mapred.TaskTracker.clienttrace: src: 10.0.0.3:50060, dest: 10.0.0.3:47317, bytes: 6, op: MAPRED$
2016-02-27 16:00:07,599 INFO org.apache.hadoop.mapred.TaskTracker: attempt_201602271541_0001_r_000000_0 0.16666667% reduce > copy (1 of 2 at 0$
2016-02-27 16:00:10,602 INFO org.apache.hadoop.mapred.TaskTracker: attempt_201602271541_0001_r_000000_0 0.16666667% reduce > copy (1 of 2 at 0$
2016-02-27 16:00:16,605 INFO org.apache.hadoop.mapred.TaskTracker: attempt_201602271541_0001_r_000000_0 0.16666667% reduce > copy (1 of 2 at 0$
2016-02-27 16:00:22,609 INFO org.apache.hadoop.mapred.TaskTracker: attempt_201602271541_0001_r_000000_0 0.16666667% reduce > copy (1 of 2 at 0$
$2 at 0.00 MB/s) >
释放它。