001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
029     * @author Shuyang Zhou
030     */
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    }