-
Notifications
You must be signed in to change notification settings - Fork 135
VELOCITY-989 Fixed multiple connection leaks in DataSourceResourceLoader. #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FrancisS
wants to merge
6
commits into
apache:master
Choose a base branch
from
FrancisS:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ea6b4c
Fixed multiple connection leaks in DataSourceResourceLoader.
FrancisS 81f4aec
Added explicit test for not finding template.
FrancisS d939514
Added proxy for PreparedStatement and ResultSet that guarantees Prepa…
FrancisS 197cf44
Added some class comments.
FrancisS 62cecee
type consistency.
FrancisS 2e95f70
Formatting.
FrancisS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ore/src/main/java/org/apache/velocity/runtime/resource/loader/PreparedStatementProxy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package org.apache.velocity.runtime.resource.loader; | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| /** | ||
| * Proxy for {@link java.sql.PreparedStatement} that guarantees getConnection will always return the DataSource | ||
| * provided wrapped {@link java.sql.Connection} and not the underlying database specific connection. | ||
| * Also overrides executeQuery to return a proxy for {@link java.sql.ResultSet} see {@link ResultSetProxy} | ||
| */ | ||
| public class PreparedStatementProxy implements InvocationHandler { | ||
| private final PreparedStatement wrappedPreparedStatement; | ||
| private PreparedStatement proxyPreparedStatement; | ||
| private final Connection wrappedConnection; | ||
|
|
||
| public PreparedStatementProxy(PreparedStatement wrappedPreparedStatement, Connection wrappedConnection) { | ||
| this.wrappedPreparedStatement = wrappedPreparedStatement; | ||
| this.wrappedConnection = wrappedConnection; | ||
| } | ||
|
|
||
| @Override | ||
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
| Object result; | ||
| if (method.getName().equals("getConnection")) { | ||
| result = wrappedConnection; | ||
| } else if (method.getName().equals("executeQuery")) { | ||
| return ResultSetProxy.newInstance((ResultSet) method.invoke(wrappedPreparedStatement, args), proxyPreparedStatement); | ||
| } else { | ||
| result = method.invoke(wrappedPreparedStatement, args); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static PreparedStatement newInstance(PreparedStatement ps, Connection con) throws SQLException { | ||
| PreparedStatementProxy invocationHandler = new PreparedStatementProxy(ps, con); | ||
| PreparedStatement proxyPreparedStatement = (PreparedStatement) Proxy.newProxyInstance(ps.getClass().getClassLoader(), | ||
| ps.getClass().getInterfaces(), | ||
| invocationHandler); | ||
| invocationHandler.setProxyPreparedStatement(proxyPreparedStatement); | ||
| return proxyPreparedStatement; | ||
| } | ||
|
|
||
| public void setProxyPreparedStatement(PreparedStatement proxyPreparedStatement) { | ||
| this.proxyPreparedStatement = proxyPreparedStatement; | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
...engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/ResultSetProxy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package org.apache.velocity.runtime.resource.loader; | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * Proxy for {@link java.sql.ResultSet} that guarantees getStatement will return the original wrapped {@link java.sql.Statement} | ||
| * and will not throw an exception or return null if the ResultSet is closed. | ||
| */ | ||
| public class ResultSetProxy implements InvocationHandler { | ||
| private final ResultSet wrappedResultSet; | ||
| private final Statement wrappedStatement; | ||
|
|
||
| public ResultSetProxy(ResultSet wrappedResultSet, Statement wrappedStatement) { | ||
| this.wrappedResultSet = wrappedResultSet; | ||
| this.wrappedStatement = wrappedStatement; | ||
| } | ||
|
|
||
| @Override | ||
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
| Object result; | ||
| if (method.getName().equals("getStatement")) { | ||
| result = wrappedStatement; | ||
| } else { | ||
| result = method.invoke(wrappedResultSet, args); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static ResultSet newInstance(ResultSet rs, Statement ps) throws SQLException { | ||
| return (ResultSet) Proxy.newProxyInstance(rs.getClass().getClassLoader(), | ||
| rs.getClass().getInterfaces(), | ||
| new ResultSetProxy(rs, ps)); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.