我是挂毯式休眠用户,遇到一个问题,即当我超过Executors.newFixedThreadPool(1)时,会话仍保持关闭状态。

我有以下代码,它们将在第一个线程中正常工作,而其余线程遇到封闭的会话。如果我将线程池增加到10,则所有线程都将正常运行。一旦超过了fixedThreadPool,我就会收到会话关闭的异常。我不知道如何打开它,因为它由Tapestry-Hibernate管理。如果我使用newCachedThreadPool,则一切运行正常。有人知道这里会发生什么吗?

public void setupRender() {
        ExecutorService executorService = Executors.newFixedThreadPool(1);

        final ConcurrentHashMap<String, Computer> map = new ConcurrentHashMap<>();
        final String key = "myKey";

        final Date date = new Date();

        List<Future> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            final int thread = i;

            Future future = executorService.submit(new Callable() {

                @Override
                public String call() {
                    try {
                        Computer computer = new Computer("Test Computer thread");
                        computer = getComputer(map, key, key, computer);

                        Monitor monitor = new Monitor();
                        monitor.setComputer(computer);

                        session.save(monitor);
                        session.flush();
                        System.out.println("thread " + thread);
                        try {
                            sessionManager.commit();
                        } catch (HibernateException  ex) {
                            sessionManager.abort();
                        } finally {
                            session.close();
                        }
                    } catch (Exception ex) {
                        System.out.println("ex " + ex);
                    }
                    System.out.println( new Date().getTime() - date.getTime());
                    return "completed";
                }

            });
            futures.add(future);
        }

        for(Future future : futures) {
            try {
                System.out.println(future.get());
            } catch (InterruptedException | ExecutionException ex) {
                Logger.getLogger(MultiThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public synchronized Computer getComputer(ConcurrentHashMap<String, Computer> map, String key, String thread, Computer computer) {
        if (map.putIfAbsent(key, computer) == null) {
            session.save(computer);
        } else {
            computer = map.get(key);
        }
        return computer;
    }

最佳答案

我之前已经告诉过您……。您必须使用ParallelExecutor或致电PerThreadManager.cleanup()。您需要了解,tapestry-hibernate具有PerThread作用域的服务,如果您在常规请求/响应(或ParallelExecutor)之外使用它们,则必须清除这些服务。

我也不认为您应该致电session.close()。您应该模仿CommitAfterWorker

它可能看起来像:

@Inject PerThreadManager perThreadManager;
@Inject HibernateSessionManager sessionManager; // this is a proxy to a per-thread value
@Inject Session session; // this is a proxy to a per-thread value

public void someMethod() {
    ExecutorService executorService = ...;
    executorService.submit(new Callable() {
        public String call() {
            try {
                Monitor monitor = ...
                session.save(monitor);
                session.flush(); // optional
                sessionManager.commit();
            } catch (Exception ex) {
                sessionManager.abort();
            } finally {
                // this allows Session and HibernateSessionManager to
                // clean up after themselves
                perThreadManager.cleanup();
            }
            return ...
        }
    });
}


如果选择使用ParallelExecutor(和Invokable)而不是Executors.newFixedThreadPool(1),则可以删除对PerThreadManager的引用,因为它会自动清理线程。

09-05 11:27