You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metamodel.apache.org by jakubneubauer <gi...@git.apache.org> on 2018/10/03 13:07:38 UTC

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

GitHub user jakubneubauer opened a pull request:

    https://github.com/apache/metamodel/pull/190

    Added cache for prepared statements in JdbcUpdateCallback

    Closes METAMODEL-1199

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/jakubneubauer/metamodel feature/METAMODEL-1199-jdbc-prepared-statements-cache

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/metamodel/pull/190.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #190
    
----
commit fe9fd8604c8059e57539822f30eb05e8296ad37f
Author: Jakub Neubauer <ja...@...>
Date:   2018-10-03T13:04:53Z

    METAMODEL-1199 Added cache for prepared statements in JdbcUpdateCallback. Size can be tweaked via system property.

----


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222489649
  
    --- Diff: jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcUpdateCallback.java ---
    @@ -42,18 +43,35 @@
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
    +import com.google.common.cache.CacheBuilder;
    +import com.google.common.cache.CacheLoader;
    +import com.google.common.cache.LoadingCache;
    +import javafx.util.Pair;
    --- End diff --
    
    We should not rely on javafx.


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222489367
  
    --- Diff: jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcDataContext.java ---
    @@ -903,4 +905,17 @@ public String getDatabaseProductName() {
         public String getDatabaseVersion() {
             return _databaseVersion;
         }
    +
    +    public static int getSystemPropertyValue(String property, int defaultValue) {
    --- End diff --
    
    We should probably make this protected scope. I don't think we should expose this method outside of the JDBC module.


---

[GitHub] metamodel issue #190: Added cache for prepared statements in JdbcUpdateCallb...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on the issue:

    https://github.com/apache/metamodel/pull/190
  
    Hi @jakubneubauer are you still working on this?


---

[GitHub] metamodel issue #190: Added cache for prepared statements in JdbcUpdateCallb...

Posted by jakubneubauer <gi...@git.apache.org>.
Github user jakubneubauer commented on the issue:

    https://github.com/apache/metamodel/pull/190
  
    Today I discovered one issue with this MR, which means backward incompatibility. When using batching, it means JdbcBatchUpdateCallback, this bigger cache is not safe. Resp. the order of executed statements can be changed. Because the commands are buffered (via Statement.addBatch) and they are then flushed for each prepared statement together.
    So for example, when user runs commands in this order: A, B, A, B, they are actually executed in order A, A, B, B. Moreover, it can be also B, B, A, A, because order of closing and flushing prepared statements from the cache is undefined.
    So the bigger cache is safe to use only for non-batching JdbcSimpleUpdateCallback.


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222490126
  
    --- Diff: jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcUpdateCallback.java ---
    @@ -42,18 +43,35 @@
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
    +import com.google.common.cache.CacheBuilder;
    +import com.google.common.cache.CacheLoader;
    +import com.google.common.cache.LoadingCache;
    +import javafx.util.Pair;
    --- End diff --
    
    I'd create a private type for it I guess.


---

[GitHub] metamodel issue #190: Added cache for prepared statements in JdbcUpdateCallb...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on the issue:

    https://github.com/apache/metamodel/pull/190
  
    So I suggest then only adding the cache to the non-batch update callback?


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222490606
  
    --- Diff: jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcUpdateCallback.java ---
    @@ -42,18 +43,35 @@
     import org.slf4j.Logger;
     import org.slf4j.LoggerFactory;
     
    +import com.google.common.cache.CacheBuilder;
    +import com.google.common.cache.CacheLoader;
    +import com.google.common.cache.LoadingCache;
    +import javafx.util.Pair;
    +
     abstract class JdbcUpdateCallback extends AbstractUpdateCallback implements UpdateCallback {
     
         private static final Logger logger = LoggerFactory.getLogger(JdbcUpdateCallback.class);
     
         private Connection _connection;
    -    private String _preparedStatementSql;
    -    private PreparedStatement _preparedStatement;
         private final UpdateSummaryBuilder _updateSummaryBuilder;
    +    private final LoadingCache<Pair<String, Boolean>, PreparedStatement> _preparedStatementCache;
     
         public JdbcUpdateCallback(JdbcDataContext dataContext) {
             super(dataContext);
             _updateSummaryBuilder = new UpdateSummaryBuilder();
    +        int maxCacheSize = JdbcDataContext.getSystemPropertyValue(JdbcDataContext.SYSTEM_PROPERTY_PREPARED_STATEMENT_CACHE_SIZE, 10);
    +        _preparedStatementCache = CacheBuilder.newBuilder()
    +                .maximumSize(maxCacheSize)
    +                .initialCapacity(1)
    +                .removalListener((removalNotification) -> {
    +                    closePreparedStatement((PreparedStatement) removalNotification.getValue());
    --- End diff --
    
    I'd implement the interface properly here instead of using a lambda. Then you don't have to do this cast which originates from the compiler not being able to infer the types I guess.


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222491235
  
    --- Diff: jdbc/src/test/java/org/apache/metamodel/jdbc/CloseableConnectionWrapper.java ---
    @@ -44,13 +44,12 @@
      * closed or not, without actually closing it.
      */
     @Ignore
    -public class CloseableConnectionWrapper implements Connection {
    +public class CloseableConnectionWrapper extends ConnectionWrapper {
    --- End diff --
    
    I don't understand why this change is/was needed. Can you explain?


---

[GitHub] metamodel pull request #190: Added cache for prepared statements in JdbcUpda...

Posted by kaspersorensen <gi...@git.apache.org>.
Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/190#discussion_r222489903
  
    --- Diff: jdbc/src/test/java/org/apache/metamodel/jdbc/ConnectionWrapper.java ---
    @@ -0,0 +1,286 @@
    +/**
    + * 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.metamodel.jdbc;
    +
    +import java.sql.Array;
    +import java.sql.Blob;
    +import java.sql.CallableStatement;
    +import java.sql.Clob;
    +import java.sql.Connection;
    +import java.sql.DatabaseMetaData;
    +import java.sql.NClob;
    +import java.sql.PreparedStatement;
    +import java.sql.SQLClientInfoException;
    +import java.sql.SQLException;
    +import java.sql.SQLWarning;
    +import java.sql.SQLXML;
    +import java.sql.Savepoint;
    +import java.sql.Statement;
    +import java.sql.Struct;
    +import java.util.Map;
    +import java.util.Properties;
    +import java.util.concurrent.Executor;
    +
    +/** Simple wrapper around Connection */
    --- End diff --
    
    Javadoc convention says that there should be a header and footer line, so this should become
    ```
    /**
     * Simple wrapper around Connection
     */
    ```


---