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.util;
016    
017    import com.liferay.portal.util.PropsValues;
018    
019    import java.sql.Connection;
020    import java.sql.SQLException;
021    
022    import javax.sql.DataSource;
023    
024    /**
025     * @author Matthew Tambara
026     */
027    public class RetryDataSourceWrapper extends DataSourceWrapper {
028    
029            public RetryDataSourceWrapper(DataSource dataSource) {
030                    super(dataSource);
031            }
032    
033            @Override
034            public Connection getConnection() throws SQLException {
035                    int retries = PropsValues.RETRY_DATA_SOURCE_MAX_RETRIES;
036    
037                    SQLException sqlException = null;
038    
039                    while (retries-- >= 0) {
040                            try {
041                                    return super.getConnection();
042                            }
043                            catch (SQLException sqle) {
044                                    if (sqlException == null) {
045                                            sqlException = sqle;
046                                    }
047                            }
048                    }
049    
050                    throw sqlException;
051            }
052    
053            @Override
054            public Connection getConnection(String username, String password)
055                    throws SQLException {
056    
057                    int retries = PropsValues.RETRY_DATA_SOURCE_MAX_RETRIES;
058    
059                    SQLException sqlException = null;
060    
061                    while (retries-- >= 0) {
062                            try {
063                                    return super.getConnection(username, password);
064                            }
065                            catch (SQLException sqle) {
066                                    if (sqlException == null) {
067                                            sqlException = sqle;
068                                    }
069                            }
070                    }
071    
072                    throw sqlException;
073            }
074    
075    }