1

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

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

@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
    }
}
4

0 回答 0