001
014
015 package com.liferay.portal.dao.jdbc.spring;
016
017 import com.liferay.portal.kernel.dao.jdbc.CurrentConnectionUtil;
018 import com.liferay.portal.kernel.util.ProxyUtil;
019
020 import java.lang.reflect.InvocationHandler;
021 import java.lang.reflect.Method;
022
023 import java.sql.Connection;
024 import java.sql.SQLException;
025
026 import javax.sql.DataSource;
027
028
031 public class ConnectionUtil {
032
033 public static Connection getConnection(DataSource dataSource)
034 throws SQLException {
035
036 Connection connection = CurrentConnectionUtil.getConnection(dataSource);
037
038 if (connection != null) {
039 return (Connection)ProxyUtil.newProxyInstance(
040 ClassLoader.getSystemClassLoader(), _interfaces,
041 new UncloseableInvocationHandler(connection));
042 }
043
044 return dataSource.getConnection();
045 }
046
047 private static final Method _closeMethod;
048 private static final Class<?>[] _interfaces = {Connection.class};
049
050 static {
051 try {
052 _closeMethod = Connection.class.getMethod("close");
053 }
054 catch (ReflectiveOperationException roe) {
055 throw new ExceptionInInitializerError(roe);
056 }
057 }
058
059 private static class UncloseableInvocationHandler
060 implements InvocationHandler {
061
062 @Override
063 public Object invoke(Object proxy, Method method, Object[] args)
064 throws Throwable {
065
066 if (method.equals(_closeMethod)) {
067 return null;
068 }
069
070 return method.invoke(_connection, args);
071 }
072
073 private UncloseableInvocationHandler(Connection connection) {
074 _connection = connection;
075 }
076
077 private final Connection _connection;
078
079 }
080
081 }