Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaEE中隐式关闭上下文_Java_Wildfly_Java 11_Java Ee 8 - Fatal编程技术网

在JavaEE中隐式关闭上下文

在JavaEE中隐式关闭上下文,java,wildfly,java-11,java-ee-8,Java,Wildfly,Java 11,Java Ee 8,我试图在JavaEE8(Java11)中实现以下代码,以访问Wildfly 20中的数据源。目标是隐式关闭JNDI上下文和SQL连接: try (InitialContext context = new InitialContext(); Connection conn = ((DataSource) context.lookup(pool)).getConnection()) { // use the connection } catch (NamingEx

我试图在JavaEE8(Java11)中实现以下代码,以访问Wildfly 20中的数据源。目标是隐式关闭JNDI上下文和SQL连接:

try (InitialContext context = new InitialContext();
     Connection conn = ((DataSource) context.lookup(pool)).getConnection()) {
    
      // use the connection

} 
catch (NamingException e) {
    logger.error(e.getMessage());
} 
catch (SQLException e) {
    logger.error(e.getMessage());
}
问题是上下文不可关闭,因为我得到了编译错误:

The resource type InitialContext does not implement java.lang.AutoCloseable

我试图避免添加
最后
来关闭上下文,有没有办法做到这一点?

尝试使用资源仅适用于
可自动关闭的对象。不使用
finally
的一种方法是将
InitialContext
包装在一个实现
AutoCloseable
的包装器中,该包装器在其
close()方法中执行必要的操作。大概是这样的:

public class AutoCloseableWrapper implements AutoCloseable {

    private final InitialContext context;

    public AutoCloseableWrapper(InitialContext context) {
        this.context = context;
    }

    public InitialContext getContext() {
        return this.context;
    }

    @Override
    public void close() throws Exception {
        this.context.close();
    }
}
try (AutoCloseableWrapper wrapper = new AutoCloseableWrapper(new InitialContext());
     Connection conn = ((DataSource) wrapper.getContext().lookup(pool)).getConnection()) {

    // ...
}
像这样使用它:

public class AutoCloseableWrapper implements AutoCloseable {

    private final InitialContext context;

    public AutoCloseableWrapper(InitialContext context) {
        this.context = context;
    }

    public InitialContext getContext() {
        return this.context;
    }

    @Override
    public void close() throws Exception {
        this.context.close();
    }
}
try (AutoCloseableWrapper wrapper = new AutoCloseableWrapper(new InitialContext());
     Connection conn = ((DataSource) wrapper.getContext().lookup(pool)).getConnection()) {

    // ...
}

Try with resources仅适用于可自动关闭的对象。不使用
finally
的一种方法是将
InitialContext
包装在一个实现
AutoCloseable
的包装器中,该包装器在其
close()方法中执行必要的操作。大概是这样的:

public class AutoCloseableWrapper implements AutoCloseable {

    private final InitialContext context;

    public AutoCloseableWrapper(InitialContext context) {
        this.context = context;
    }

    public InitialContext getContext() {
        return this.context;
    }

    @Override
    public void close() throws Exception {
        this.context.close();
    }
}
try (AutoCloseableWrapper wrapper = new AutoCloseableWrapper(new InitialContext());
     Connection conn = ((DataSource) wrapper.getContext().lookup(pool)).getConnection()) {

    // ...
}
像这样使用它:

public class AutoCloseableWrapper implements AutoCloseable {

    private final InitialContext context;

    public AutoCloseableWrapper(InitialContext context) {
        this.context = context;
    }

    public InitialContext getContext() {
        return this.context;
    }

    @Override
    public void close() throws Exception {
        this.context.close();
    }
}
try (AutoCloseableWrapper wrapper = new AutoCloseableWrapper(new InitialContext());
     Connection conn = ((DataSource) wrapper.getContext().lookup(pool)).getConnection()) {

    // ...
}