001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018 019package org.apache.commons.dbcp2; 020 021import java.sql.Connection; 022import java.sql.ResultSet; 023import java.sql.Statement; 024import java.text.MessageFormat; 025import java.util.HashSet; 026import java.util.ResourceBundle; 027import java.util.Set; 028 029/** 030 * Utility methods. 031 * @since 2.0 032 */ 033public final class Utils { 034 035 private static final ResourceBundle messages = ResourceBundle.getBundle( 036 Utils.class.getPackage().getName() + ".LocalStrings"); 037 038 public static final boolean IS_SECURITY_ENABLED = 039 System.getSecurityManager() != null; 040 041 /** Any SQL_STATE starting with this value is considered a fatal disconnect */ 042 public static final String DISCONNECTION_SQL_CODE_PREFIX = "08"; 043 044 /** 045 * SQL codes of fatal connection errors. 046 * <ul> 047 * <li>57P01 (ADMIN SHUTDOWN)</li> 048 * <li>57P02 (CRASH SHUTDOWN)</li> 049 * <li>57P03 (CANNOT CONNECT NOW)</li> 050 * <li>01002 (SQL92 disconnect error)</li> 051 * <li>JZ0C0 (Sybase disconnect error)</li> 052 * <li>JZ0C1 (Sybase disconnect error)</li> 053 * </ul> 054 */ 055 public static final Set<String> DISCONNECTION_SQL_CODES; 056 057 static { 058 DISCONNECTION_SQL_CODES = new HashSet<>(); 059 DISCONNECTION_SQL_CODES.add("57P01"); // ADMIN SHUTDOWN 060 DISCONNECTION_SQL_CODES.add("57P02"); // CRASH SHUTDOWN 061 DISCONNECTION_SQL_CODES.add("57P03"); // CANNOT CONNECT NOW 062 DISCONNECTION_SQL_CODES.add("01002"); // SQL92 disconnect error 063 DISCONNECTION_SQL_CODES.add("JZ0C0"); // Sybase disconnect error 064 DISCONNECTION_SQL_CODES.add("JZ0C1"); // Sybase disconnect error 065 } 066 067 private Utils() { 068 // not instantiable 069 } 070 071 /** 072 * Closes the ResultSet (which may be null). 073 * 074 * @param rset a ResultSet, may be {@code null} 075 */ 076 public static void closeQuietly(final ResultSet rset) { 077 if (rset != null) { 078 try { 079 rset.close(); 080 } catch (final Exception e) { 081 // ignored 082 } 083 } 084 } 085 086 /** 087 * Closes the Connection (which may be null). 088 * 089 * @param conn a Connection, may be {@code null} 090 */ 091 public static void closeQuietly(final Connection conn) { 092 if (conn != null) { 093 try { 094 conn.close(); 095 } catch (final Exception e) { 096 // ignored 097 } 098 } 099 } 100 101 /** 102 * Closes the Statement (which may be null). 103 * 104 * @param stmt a Statement, may be {@code null} 105 */ 106 public static void closeQuietly(final Statement stmt) { 107 if (stmt != null) { 108 try { 109 stmt.close(); 110 } catch (final Exception e) { 111 // ignored 112 } 113 } 114 } 115 116 117 /** 118 * Obtain the correct i18n message for the given key. 119 */ 120 public static String getMessage(final String key) { 121 return getMessage(key, (Object[]) null); 122 } 123 124 125 /** 126 * Obtain the correct i18n message for the given key with placeholders 127 * replaced by the supplied arguments. 128 */ 129 public static String getMessage(final String key, final Object... args) { 130 final String msg = messages.getString(key); 131 if (args == null || args.length == 0) { 132 return msg; 133 } 134 final MessageFormat mf = new MessageFormat(msg); 135 return mf.format(args, new StringBuffer(), null).toString(); 136 } 137}