{"action":"created","comment":{"url":"https://api.github.com/repos/opower/jpile/pulls/comments/59859287","id":59859287,"diff_hunk":"@@ -2,68 +2,123 @@\n \n import com.google.common.base.Preconditions;\n import com.google.common.base.Throwables;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n \n import javax.sql.DataSource;\n+import java.sql.Connection;\n import java.sql.SQLException;\n \n /**\n- * This implementation operates the {@link DataSource}. It creates a\n- * {@link ConnectionBasedStatementExecutor} using {@link java.sql.Connection}\n- * from {@link DataSource} and delegates all calls to it.\n- * <p/>\n- * This implementation has no mechanism to process failures from underlying\n- * {@link ConnectionBasedStatementExecutor} and retry execution with another\n- * {@link java.sql.Connection}. If you need such behavior then create a subclass.\n- * <p/>\n- * Be aware that this implementation is not thread-safe.\n+ * <p>\n+ *  This implementation operates the {@link DataSource}. It creates a\n+ *  {@link ConnectionBasedStatementExecutor} using {@link java.sql.Connection}\n+ *  from {@link DataSource} and delegates all calls to it.\n+ * </p>\n+ * <p>\n+ *  Be aware that this implementation is not thread-safe.\n+ * </p>\n  *\n  * @author ivan.german\n  */\n public class DataSourceBasedStatementExecutor implements StatementExecutor {\n+    private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceBasedStatementExecutor.class);\n \n     private ConnectionBasedStatementExecutor connectionBasedStatementExecutor;\n     private final DataSource dataSource;\n \n-    public DataSourceBasedStatementExecutor(DataSource dataSource) throws SQLException {\n+    public DataSourceBasedStatementExecutor(DataSource dataSource) {\n         Preconditions.checkNotNull(dataSource, \"dataSource must not be null\");\n \n         this.dataSource = dataSource;\n-        initNewConnection();\n     }\n \n     /**\n-     * {@inheritDoc}\n+     * <p>\n+     *  Processes {@link StatementCallback} in context of used connection.\n+     *  If this {@link StatementExecutor} don't own any open connection then new one\n+     *  is requested from the data source.\n+     * </p>\n+     * <p>\n+     *  Be aware that foreign key constraints are disabled for used connection.\n+     * </p>\n+     *\n+     * @throws RuntimeException if any problem occur during execution\n+     * @return result of {@link StatementCallback}\n      */\n     @Override\n     public <T> T execute(StatementCallback<T> statementCallback) {\n-        Preconditions.checkNotNull(\n-                this.connectionBasedStatementExecutor, \"underlying connectionBasedStatementExecutor is null\");\n+        try {\n+            if (this.connectionBasedStatementExecutor == null) {\n+                initNewConnection();\n+            }\n \n-        return this.connectionBasedStatementExecutor.execute(statementCallback);\n+            return this.connectionBasedStatementExecutor.execute(statementCallback);\n+        }\n+        /* Propagated exception */\n+        catch (Exception e) {\n+            shutdown();\n+            throw Throwables.propagate(e);\n+        }","path":"src/main/java/com/opower/persistence/jpile/jdbc/DataSourceBasedStatementExecutor.java","position":76,"original_position":76,"commit_id":"5e8e0e96335ea26d1f497fc8342ade8bc58ffeb2","original_commit_id":"5e8e0e96335ea26d1f497fc8342ade8bc58ffeb2","user":{"login":"io-german","id":4814196,"avatar_url":"https://avatars.githubusercontent.com/u/4814196?v=3","gravatar_id":"","url":"https://api.github.com/users/io-german","html_url":"https://github.com/io-german","followers_url":"https://api.github.com/users/io-german/followers","following_url":"https://api.github.com/users/io-german/following{/other_user}","gists_url":"https://api.github.com/users/io-german/gists{/gist_id}","starred_url":"https://api.github.com/users/io-german/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/io-german/subscriptions","organizations_url":"https://api.github.com/users/io-german/orgs","repos_url":"https://api.github.com/users/io-german/repos","events_url":"https://api.github.com/users/io-german/events{/privacy}","received_events_url":"https://api.github.com/users/io-german/received_events","type":"User","site_admin":false},"body":"Thanks for the example! I haven't ever used ```addSuppressed()``` method before but it's usage here seems very natural.","created_at":"2016-04-15T11:07:56Z","updated_at":"2016-04-15T11:07:56Z","html_url":"https://github.com/opower/jpile/pull/51#discussion_r59859287","pull_request_url":"https://api.github.com/repos/opower/jpile/pulls/51","_links":{"self":{"href":"https://api.github.com/repos/opower/jpile/pulls/comments/59859287"},"html":{"href":"https://github.com/opower/jpile/pull/51#discussion_r59859287"},"pull_request":{"href":"https://api.github.com/repos/opower/jpile/pulls/51"}}},"pull_request":{"url":"https://api.github.com/repos/opower/jpile/pulls/51","id":66310069,"html_url":"https://github.com/opower/jpile/pull/51","diff_url":"https://github.com/opower/jpile/pull/51.diff","patch_url":"https://github.com/opower/jpile/pull/51.patch","issue_url":"https://api.github.com/repos/opower/jpile/issues/51","number":51,"state":"open","locked":false,"title":"Fix several design issues related to DataSourceBasedStatementExecutor class","user":{"login":"io-german","id":4814196,"avatar_url":"https://avatars.githubusercontent.com/u/4814196?v=3","gravatar_id":"","url":"https://api.github.com/users/io-german","html_url":"https://github.com/io-german","followers_url":"https://api.github.com/users/io-german/followers","following_url":"https://api.github.com/users/io-german/following{/other_user}","gists_url":"https://api.github.com/users/io-german/gists{/gist_id}","starred_url":"https://api.github.com/users/io-german/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/io-german/subscriptions","organizations_url":"https://api.github.com/users/io-german/orgs","repos_url":"https://api.github.com/users/io-german/repos","events_url":"https://api.github.com/users/io-german/events{/privacy}","received_events_url":"https://api.github.com/users/io-german/received_events","type":"User","site_admin":false},"body":"Trying to implement advanced ```StatementExecutor``` using ```DataSourceBasedStatementExecutor``` we have encountered several design issues of this class.\r\n\r\n1. Queries that disable/enable foreign key constraints can become a source of connection leaks because there was no code to handle failures that might happen in these queries.\r\n2. Side-effect in constructor (```DataSource.getConnection()```) make it difficult to test subclasses and wrappers for this class.\r\n\r\nHere are the fixes for found issues:\r\n1. Guarantee that connections are closed.\r\n2. Remove side-effect from constructor. Lazily initialize connection before call to execute.\r\n3. Expand both unit and integration test sets for ```DataSourceBasedStatementExecutor```.","created_at":"2016-04-13T12:07:52Z","updated_at":"2016-04-15T11:07:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e205905cfbab6dfb3b89dd082fc55bf4e0a67ecb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/opower/jpile/pulls/51/commits","review_comments_url":"https://api.github.com/repos/opower/jpile/pulls/51/comments","review_comment_url":"https://api.github.com/repos/opower/jpile/pulls/comments{/number}","comments_url":"https://api.github.com/repos/opower/jpile/issues/51/comments","statuses_url":"https://api.github.com/repos/opower/jpile/statuses/5e8e0e96335ea26d1f497fc8342ade8bc58ffeb2","head":{"label":"io-german:fix_datasource_based_statement_executor","ref":"fix_datasource_based_statement_executor","sha":"5e8e0e96335ea26d1f497fc8342ade8bc58ffeb2","user":{"login":"io-german","id":4814196,"avatar_url":"https://avatars.githubusercontent.com/u/4814196?v=3","gravatar_id":"","url":"https://api.github.com/users/io-german","html_url":"https://github.com/io-german","followers_url":"https://api.github.com/users/io-german/followers","following_url":"https://api.github.com/users/io-german/following{/other_user}","gists_url":"https://api.github.com/users/io-german/gists{/gist_id}","starred_url":"https://api.github.com/users/io-german/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/io-german/subscriptions","organizations_url":"https://api.github.com/users/io-german/orgs","repos_url":"https://api.github.com/users/io-german/repos","events_url":"https://api.github.com/users/io-german/events{/privacy}","received_events_url":"https://api.github.com/users/io-german/received_events","type":"User","site_admin":false},"repo":{"id":51303240,"name":"jpile","full_name":"io-german/jpile","owner":{"login":"io-german","id":4814196,"avatar_url":"https://avatars.githubusercontent.com/u/4814196?v=3","gravatar_id":"","url":"https://api.github.com/users/io-german","html_url":"https://github.com/io-german","followers_url":"https://api.github.com/users/io-german/followers","following_url":"https://api.github.com/users/io-german/following{/other_user}","gists_url":"https://api.github.com/users/io-german/gists{/gist_id}","starred_url":"https://api.github.com/users/io-german/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/io-german/subscriptions","organizations_url":"https://api.github.com/users/io-german/orgs","repos_url":"https://api.github.com/users/io-german/repos","events_url":"https://api.github.com/users/io-german/events{/privacy}","received_events_url":"https://api.github.com/users/io-german/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/io-german/jpile","description":"Java Persistence Infile LoadEr","fork":true,"url":"https://api.github.com/repos/io-german/jpile","forks_url":"https://api.github.com/repos/io-german/jpile/forks","keys_url":"https://api.github.com/repos/io-german/jpile/keys{/key_id}","collaborators_url":"https://api.github.com/repos/io-german/jpile/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/io-german/jpile/teams","hooks_url":"https://api.github.com/repos/io-german/jpile/hooks","issue_events_url":"https://api.github.com/repos/io-german/jpile/issues/events{/number}","events_url":"https://api.github.com/repos/io-german/jpile/events","assignees_url":"https://api.github.com/repos/io-german/jpile/assignees{/user}","branches_url":"https://api.github.com/repos/io-german/jpile/branches{/branch}","tags_url":"https://api.github.com/repos/io-german/jpile/tags","blobs_url":"https://api.github.com/repos/io-german/jpile/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/io-german/jpile/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/io-german/jpile/git/refs{/sha}","trees_url":"https://api.github.com/repos/io-german/jpile/git/trees{/sha}","statuses_url":"https://api.github.com/repos/io-german/jpile/statuses/{sha}","languages_url":"https://api.github.com/repos/io-german/jpile/languages","stargazers_url":"https://api.github.com/repos/io-german/jpile/stargazers","contributors_url":"https://api.github.com/repos/io-german/jpile/contributors","subscribers_url":"https://api.github.com/repos/io-german/jpile/subscribers","subscription_url":"https://api.github.com/repos/io-german/jpile/subscription","commits_url":"https://api.github.com/repos/io-german/jpile/commits{/sha}","git_commits_url":"https://api.github.com/repos/io-german/jpile/git/commits{/sha}","comments_url":"https://api.github.com/repos/io-german/jpile/comments{/number}","issue_comment_url":"https://api.github.com/repos/io-german/jpile/issues/comments{/number}","contents_url":"https://api.github.com/repos/io-german/jpile/contents/{+path}","compare_url":"https://api.github.com/repos/io-german/jpile/compare/{base}...{head}","merges_url":"https://api.github.com/repos/io-german/jpile/merges","archive_url":"https://api.github.com/repos/io-german/jpile/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/io-german/jpile/downloads","issues_url":"https://api.github.com/repos/io-german/jpile/issues{/number}","pulls_url":"https://api.github.com/repos/io-german/jpile/pulls{/number}","milestones_url":"https://api.github.com/repos/io-german/jpile/milestones{/number}","notifications_url":"https://api.github.com/repos/io-german/jpile/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/io-german/jpile/labels{/name}","releases_url":"https://api.github.com/repos/io-german/jpile/releases{/id}","deployments_url":"https://api.github.com/repos/io-german/jpile/deployments","created_at":"2016-02-08T14:40:51Z","updated_at":"2016-02-08T14:40:52Z","pushed_at":"2016-04-13T11:49:47Z","git_url":"git://github.com/io-german/jpile.git","ssh_url":"git@github.com:io-german/jpile.git","clone_url":"https://github.com/io-german/jpile.git","svn_url":"https://github.com/io-german/jpile","homepage":"","size":357,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"opower:master","ref":"master","sha":"e8a7b024032723591ffd6e62c51e64bbacb8ae00","user":{"login":"opower","id":636633,"avatar_url":"https://avatars.githubusercontent.com/u/636633?v=3","gravatar_id":"","url":"https://api.github.com/users/opower","html_url":"https://github.com/opower","followers_url":"https://api.github.com/users/opower/followers","following_url":"https://api.github.com/users/opower/following{/other_user}","gists_url":"https://api.github.com/users/opower/gists{/gist_id}","starred_url":"https://api.github.com/users/opower/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opower/subscriptions","organizations_url":"https://api.github.com/users/opower/orgs","repos_url":"https://api.github.com/users/opower/repos","events_url":"https://api.github.com/users/opower/events{/privacy}","received_events_url":"https://api.github.com/users/opower/received_events","type":"Organization","site_admin":false},"repo":{"id":2944043,"name":"jpile","full_name":"opower/jpile","owner":{"login":"opower","id":636633,"avatar_url":"https://avatars.githubusercontent.com/u/636633?v=3","gravatar_id":"","url":"https://api.github.com/users/opower","html_url":"https://github.com/opower","followers_url":"https://api.github.com/users/opower/followers","following_url":"https://api.github.com/users/opower/following{/other_user}","gists_url":"https://api.github.com/users/opower/gists{/gist_id}","starred_url":"https://api.github.com/users/opower/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/opower/subscriptions","organizations_url":"https://api.github.com/users/opower/orgs","repos_url":"https://api.github.com/users/opower/repos","events_url":"https://api.github.com/users/opower/events{/privacy}","received_events_url":"https://api.github.com/users/opower/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/opower/jpile","description":"Java Persistence Infile LoadEr","fork":false,"url":"https://api.github.com/repos/opower/jpile","forks_url":"https://api.github.com/repos/opower/jpile/forks","keys_url":"https://api.github.com/repos/opower/jpile/keys{/key_id}","collaborators_url":"https://api.github.com/repos/opower/jpile/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/opower/jpile/teams","hooks_url":"https://api.github.com/repos/opower/jpile/hooks","issue_events_url":"https://api.github.com/repos/opower/jpile/issues/events{/number}","events_url":"https://api.github.com/repos/opower/jpile/events","assignees_url":"https://api.github.com/repos/opower/jpile/assignees{/user}","branches_url":"https://api.github.com/repos/opower/jpile/branches{/branch}","tags_url":"https://api.github.com/repos/opower/jpile/tags","blobs_url":"https://api.github.com/repos/opower/jpile/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/opower/jpile/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/opower/jpile/git/refs{/sha}","trees_url":"https://api.github.com/repos/opower/jpile/git/trees{/sha}","statuses_url":"https://api.github.com/repos/opower/jpile/statuses/{sha}","languages_url":"https://api.github.com/repos/opower/jpile/languages","stargazers_url":"https://api.github.com/repos/opower/jpile/stargazers","contributors_url":"https://api.github.com/repos/opower/jpile/contributors","subscribers_url":"https://api.github.com/repos/opower/jpile/subscribers","subscription_url":"https://api.github.com/repos/opower/jpile/subscription","commits_url":"https://api.github.com/repos/opower/jpile/commits{/sha}","git_commits_url":"https://api.github.com/repos/opower/jpile/git/commits{/sha}","comments_url":"https://api.github.com/repos/opower/jpile/comments{/number}","issue_comment_url":"https://api.github.com/repos/opower/jpile/issues/comments{/number}","contents_url":"https://api.github.com/repos/opower/jpile/contents/{+path}","compare_url":"https://api.github.com/repos/opower/jpile/compare/{base}...{head}","merges_url":"https://api.github.com/repos/opower/jpile/merges","archive_url":"https://api.github.com/repos/opower/jpile/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/opower/jpile/downloads","issues_url":"https://api.github.com/repos/opower/jpile/issues{/number}","pulls_url":"https://api.github.com/repos/opower/jpile/pulls{/number}","milestones_url":"https://api.github.com/repos/opower/jpile/milestones{/number}","notifications_url":"https://api.github.com/repos/opower/jpile/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/opower/jpile/labels{/name}","releases_url":"https://api.github.com/repos/opower/jpile/releases{/id}","deployments_url":"https://api.github.com/repos/opower/jpile/deployments","created_at":"2011-12-08T23:46:52Z","updated_at":"2015-12-07T04:01:37Z","pushed_at":"2016-04-13T12:07:52Z","git_url":"git://github.com/opower/jpile.git","ssh_url":"git@github.com:opower/jpile.git","clone_url":"https://github.com/opower/jpile.git","svn_url":"https://github.com/opower/jpile","homepage":"","size":419,"stargazers_count":12,"watchers_count":12,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":1,"forks":17,"open_issues":1,"watchers":12,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/opower/jpile/pulls/51"},"html":{"href":"https://github.com/opower/jpile/pull/51"},"issue":{"href":"https://api.github.com/repos/opower/jpile/issues/51"},"comments":{"href":"https://api.github.com/repos/opower/jpile/issues/51/comments"},"review_comments":{"href":"https://api.github.com/repos/opower/jpile/pulls/51/comments"},"review_comment":{"href":"https://api.github.com/repos/opower/jpile/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/opower/jpile/pulls/51/commits"},"statuses":{"href":"https://api.github.com/repos/opower/jpile/statuses/5e8e0e96335ea26d1f497fc8342ade8bc58ffeb2"}}}}