You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/03/30 08:34:29 UTC

[GitHub] [ignite] AMashenkov commented on a change in pull request #7569: IGNITE-12804 SQL: ConnectionManager refactoring

AMashenkov commented on a change in pull request #7569: IGNITE-12804  SQL: ConnectionManager refactoring
URL: https://github.com/apache/ignite/pull/7569#discussion_r400015937
 
 

 ##########
 File path: modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2PooledConnection.java
 ##########
 @@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.h2;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Pooled connection wrapper to use close semantic to recycle connection (return to the pool).
+ */
+public class H2PooledConnection implements AutoCloseable {
+    /** */
+    private volatile H2Connection delegate;
+
+    /** Connection manager. */
+    private final ConnectionManager connMgr;
+
+    /** Closed (recycled) flag. */
+    private final AtomicBoolean closed = new AtomicBoolean();
+
+    /**
+     * @param conn Connection to use.
+     * @param connMgr Connection manager is use to recycle connection
+     *      (connection is closed or returned to connection pool).
+     */
+    H2PooledConnection(H2Connection conn, ConnectionManager connMgr) {
+        this.delegate = conn;
+        this.connMgr = connMgr;
+    }
+
+    /**
+     * @return Schema name if schema is set, null otherwise.
+     */
+    public String schema() {
+        return delegate.schema();
+    }
+
+    /**
+     * @param schema Schema name set on this connection.
+     */
+    public void schema(@Nullable String schema) {
+        delegate.schema(schema);
+    }
+
+    /**
+     * @return Connection.
+     */
+    public Connection connection() {
+        return delegate.connection();
+    }
+
+    /**
+     * @return Statement cache size.
+     */
+    public int statementCacheSize() {
+        return delegate.statementCacheSize();
+    }
+
+    /**
+     * Prepare statement caching it if needed.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatement(String sql, byte qryFlags) throws IgniteCheckedException {
+        return delegate.prepareStatement(sql, qryFlags);
+    }
+
+    /**
+     * Get prepared statement without caching.
+     *
+     * @param sql SQL.
+     * @return Prepared statement.
+     */
+    public PreparedStatement prepareStatementNoCache(String sql) throws IgniteCheckedException {
+        boolean insertHack = GridH2Table.insertHackRequired(sql);
+
+        if (insertHack) {
+            GridH2Table.insertHack(true);
+
+            try {
+                return delegate.prepareStatementNoCache(sql);
+            }
+            finally {
+                GridH2Table.insertHack(false);
+            }
+        }
+        else
+            return delegate.prepareStatementNoCache(sql);
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(H2PooledConnection.class, this);
+    }
+
+    /** Closes wrapped connection (return to pool or close). */
+    @Override public void close() {
+        assert delegate != null;
+
+        if (closed.compareAndSet(false, true)) {
+            // clear thread local
+            // H2Utils.resetSession(this);
 
 Review comment:
   Is it still actual?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services