读书人

JPPF Task种中做到重复使用一个数据库

发布时间: 2012-10-31 14:37:31 作者: rapoo

JPPF Task类中做到重复使用一个数据库连接池(或者其他重量的对象)
我们可能会在有一些JPPF task中需要连接到数据库, 但是我们又不能把connection作为JPPFTask的参数或者属性(因为Connection不能序列化). 这样的化我们就只能在JPPFTask里的run方法中去创建连接. 因为我们可能有很多JPPF节点.而且每个节点也可能会的接受到很多次task. 所以就会造成频繁的创建Connection. Mysql很快的就会撑不住了.

下面介绍一个类用来解决这种问题.

Class: org.jppf.node.NodeRunner

public class XXXXXXXXXTask extends JPPFTask{    private static final String DATA_SOURCE_KEY = "dataSource";    public void run() {        DataSource dataSource = (DataSource) NodeRunner.getPersistentData(DATA_SOURCE_KEY);        XXXXXService service = null;        if (dataSource == null) {            dataSource = new DriverManagerDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://192.168.1.90:3306/xxxx?useUnicode=true&characterEncoding=UTF-8", "xxx", "xx");            service = new XXXXXService();            service.setDataSource(dataSource);            NodeRunner.setPersistentData(DATA_SOURCE_KEY);         /////  Process the task using the Service build from the dataSource./////////        }}


OK!

读书人网 >其他数据库

热点推荐