从spring transactional方法调用静态方法时的连接泄漏问题

时间:2016-05-14 18:52:28

标签: spring-data spring-transactions transactional hikaricp connection-leaks

我在Service层有一个事务方法,它调用一个静态方法,我正在为一些异步任务(再次是一些数据库操作)启动一个线程。从事务范围调用静态方法是否会导致某些数据库连接泄漏问题?

以下是解释用例的示例代码。

@Override
@Transactional
public void createRecords(Record r1, Record r2, HttpServletRequest request) {

saveSomeData(r1,r2.getScore());

// Call a Static Method that open a seperate thread
AsyncTasker.doSomeThing(r2);

saveNewData(r2);
}

AsyncTasker.java

public class AsyncTasker {

    private static final Logger logger = LoggerFactory.getLogger(AsyncTasker.class);

    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void doSomeThing(Record r){

        try{
            ClearTask clearTask = new ClearTask(r);
            executorService.submit(clearTask);
        }
        catch(Exception e){
            logger.error("Error in ClearTask task execution " + r +" : Exception "+ e);
        }
    }

}

class ClearTask implements Runnable{

    private Record r;

    ClearTask(Record r){
        this.r = r;
    }

    @Override`enter code here`
    public void run() {
        Token.deleteAllData(r); // Static method making a rest call
    }
}

0 个答案:

没有答案