当调用JooQ DSLContext的.close()时,Connection什么时候关闭?

时间:2015-11-08 13:09:30

标签: java postgresql jooq

这是我正在编写的使用JooQ 3.7.0的类的代码(不相关的部分已被剥离);请注意AutoCloseable的{​​{1}}功能的使用:

DSLContext

如果相关,则使用的数据库是PostgreSQL(9.4.x)。

在上面的代码中,我有两个断点。当我调试时,我看到了:

  • 在第一个断点处,public final class JooqPerMethodMetricWriter implements PerMethodMetricWriter { private static final Logger LOGGER = LoggerFactory.getLogger(JooqPerMethodMetricWriter.class); // [snip] private final DSLContext jooq; public JooqPerMethodMetricWriter(final Connection connection, final Instant instant) throws IOException { // [snip] jooq = DSL.using(connection); } private void writeCsv(final Configuration configuration) { // [snip] try ( final DSLContext context = DSL.using(configuration); final Reader reader = Files.newBufferedReader(csvPath); ) { final Loader<PermethodMetricsRecord> loader = context .loadInto(PERMETHOD_METRICS) .loadCSV(reader) .fields(PERMETHOD_METRICS.fields()) .execute(); LOGGER.info("{} lines stored in database", loader.stored()); } catch (IOException e) { throw new RuntimeException("Cannot open CSV for reading", e); } // BREAKPOINT 1 } @Override public void close() throws IOException { jooq.transaction(this::writeCsv); jooq.close(); // BREAKPOINT 2 Files.delete(csvPath); } // [snip] } 是假的......
  • 在第二个断点处, configuration.connectionProvider().acquire().isClosed() 也是 false。

我很困惑。我作为构造函数参数收到的jooq.configuration().connectionProvider().acquire().isClosed()发生了什么?我自己需要Connection吗?

附带问题,这次关于.close():我保留默认值,因此Loader;鉴于我在交易中运行加载程序,如果我尝试.commitNone()代替.commit<somethingElse>(),它会有所作为吗?

1 个答案:

答案 0 :(得分:1)

DSLContext使用jOOQ 3.7的Java 8发行版成为AutoCloseableDSLContext.close()方法的Javadoc解释了此close()调用的语义:

  

如果在构造此DSLContext时已分配任何资源,请关闭基础资源。

     

某些DSLContext构造函数(例如DSL.using(String)DSL.using(String, Properties)DSL.using(String, String, String)分配Connection资源,DSLContext资源无法访问close() 1}}实施。因此,必须通过此DSLContext方法进行适当的资源管理。

只有在构建DSLContext时分配的资源才会被释放。不是您传递给try-with-resources的资源。在您的情况下,您没有在此try (DSLContext context = DSL.using(configuration); ...) { ... } 语句中分配任何资源,因此在结束时没有任何内容可以发布:

Connection

如果您在那里分配了新的try (DSLContext context = DSL.using("jdbc:h2:~/test", "sa", ""); ...) { ... } ,情况会有所不同:

var ball = SKSpriteNode()

 override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.physicsWorld.gravity = CGVectorMake(0.0, -9.81)


    // Add the ball

    ball = SKSpriteNode(imageNamed: "Ball")
    ball.setScale(2.0)
    ball.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
    ball.zPosition = 100.0

    // Physics of the ball
    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.height / 2)
    ball.physicsBody?.allowsRotation = true
    ball.physicsBody?.dynamic = true
    ball.physicsBody?.usesPreciseCollisionDetection = true
    ball.physicsBody?.mass = 2.0
    ball.physicsBody?.restitution = 0.8
    ball.physicsBody?.velocity = CGVectorMake(0.0, 5.0)

    self.addChild(ball)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    self.ball.physicsBody?.applyImpulse(CGVectorMake(0.0, 5.0))



}

关于你的问题:

  

我很困惑。作为构造函数参数,我收到的Connection发生了什么?

无。您必须自己管理其生命周期,因为jOOQ对您的连接生命周期策略一无所知。

  

我自己需要.close()吗?