src/java.sql/share/classes/java/sql2/JdbcConnectionProperty.java
branchJDK-8188051-branch
changeset 56380 f06946e00a26
parent 56373 1f76a5f8e999
child 56381 653b066f4a88
equal deleted inserted replaced
56373:1f76a5f8e999 56380:f06946e00a26
     1 /*
       
     2  * Copyright (c)  2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package java.sql2;
       
    27 
       
    28 import java.util.function.Function;
       
    29 import java.time.Duration;
       
    30 
       
    31 /**
       
    32  * A set of {@link ConnectionProperty}  commonly supported. Implementations are not
       
    33  * required to support all of these properties.
       
    34  *
       
    35  */
       
    36 public enum JdbcConnectionProperty implements ConnectionProperty {
       
    37   
       
    38   /**
       
    39    *
       
    40    */
       
    41   CACHING(Caching.class, 
       
    42           v -> v instanceof Caching,
       
    43           Caching.CACHED,
       
    44           false),
       
    45 
       
    46   /**
       
    47    *
       
    48    */
       
    49   HOLDABILITY(Holdability.class, 
       
    50           v -> v instanceof Holdability,
       
    51           Holdability.HOLD_OVER_COMMIT,
       
    52           false),
       
    53 
       
    54   /**
       
    55    *
       
    56    */
       
    57   NETWORK_TIMEOUT(Integer.class, 
       
    58           v -> v instanceof Integer && ((int) v) >= 0,
       
    59           Integer.MAX_VALUE,
       
    60           false),
       
    61 
       
    62   /**
       
    63    *
       
    64    */
       
    65   PASSWORD(String.class,
       
    66           v -> v instanceof String,
       
    67           null,
       
    68           true),
       
    69 
       
    70   /**
       
    71    *
       
    72    */
       
    73   READ_ONLY(Boolean.class, 
       
    74           v -> v instanceof Boolean,
       
    75           false,
       
    76           false),
       
    77 
       
    78   /**
       
    79    *
       
    80    */
       
    81   SHARDING_KEY(ShardingKey.class,
       
    82           v -> v instanceof ShardingKey,
       
    83           null,
       
    84           false),
       
    85 
       
    86   /**
       
    87    *
       
    88    */
       
    89   SHARDING_GROUP_KEY(ShardingKey.class,
       
    90           v -> v instanceof ShardingKey,
       
    91           null,
       
    92           false),
       
    93 
       
    94   /**
       
    95    * 
       
    96    */
       
    97   CONNECT_TIMEOUT(Duration.class,
       
    98           v -> v instanceof Duration && ! ((Duration)v).isNegative(),
       
    99           Duration.ofSeconds(Long.MAX_VALUE),
       
   100           false),
       
   101   
       
   102   /**
       
   103    *
       
   104    */
       
   105   TRANSACTION_ISOLATION(TransactionIsolation.class, 
       
   106           v -> v instanceof TransactionIsolation,
       
   107           TransactionIsolation.READ_COMMITTED,
       
   108           false),
       
   109 
       
   110   /**
       
   111    *
       
   112    */
       
   113   URL(String.class,
       
   114           v -> v instanceof String,
       
   115           null,
       
   116           false),
       
   117 
       
   118   /**
       
   119    *
       
   120    */
       
   121   USER(String.class,
       
   122           v -> v instanceof String,
       
   123           null,
       
   124           false);
       
   125 
       
   126   private final Class range;
       
   127   private final Function<Object, Boolean> validator;
       
   128   private final Object defaultValue;
       
   129   private final boolean isSensitive;
       
   130 
       
   131   private JdbcConnectionProperty(Class range, 
       
   132           Function<Object, Boolean> validator,
       
   133           Object value,
       
   134           boolean isSensitive) {
       
   135     this.range = range;
       
   136     this.validator = validator;
       
   137     this.defaultValue = value;
       
   138     this.isSensitive = isSensitive;
       
   139   }
       
   140 
       
   141   @Override
       
   142   public Class range() {
       
   143     return range;
       
   144   }
       
   145 
       
   146   @Override
       
   147   public boolean validate(Object value) {
       
   148     return validator.apply(value);
       
   149   }
       
   150 
       
   151   @Override
       
   152   public Object defaultValue() {
       
   153     return null;
       
   154   }
       
   155 
       
   156   @Override
       
   157   public boolean isSensitive() {
       
   158     throw new UnsupportedOperationException("Not supported yet.");
       
   159   }
       
   160 
       
   161   /**
       
   162    *
       
   163    */
       
   164   public enum Caching {
       
   165     /**
       
   166      * The returned {@link Connection} is required to be completely new and configured
       
   167      * exactly as specified by the other properties. Use this with caution and
       
   168      * only when absolutely necessary. Use {@link AS_NEW} instead if at all possible.
       
   169      * This should be used only to work around some limitation of the database
       
   170      * or the implementation.
       
   171      */
       
   172     NEW,
       
   173     /**
       
   174      * The returned {@link Connection} has no state other than that of a new Connection
       
   175      * modified as specified by the other properties. May not be strictly new
       
   176      * but has the same behavior as a new {@link Connection}. The {@link Connection} may be {@link NEW}. The default.
       
   177      */
       
   178     AS_NEW,
       
   179     /**
       
   180      * The returned {@link Connection} has the state specified by the other properties
       
   181      * but may have additional state that differs from that of a new {@link Connection}.
       
   182      * The {@link Connection} may be {@link AS_NEW}.
       
   183      */
       
   184     CACHED;
       
   185   }
       
   186 
       
   187   /**
       
   188    *
       
   189    */
       
   190   public enum TransactionIsolation {
       
   191 
       
   192     /**
       
   193      *
       
   194      */
       
   195     NONE,
       
   196 
       
   197     /**
       
   198      *
       
   199      */
       
   200     READ_COMMITTED,
       
   201 
       
   202     /**
       
   203      *
       
   204      */
       
   205     READ_UNCOMMITTED,
       
   206 
       
   207     /**
       
   208      *
       
   209      */
       
   210     REPEATABLE_READ,
       
   211 
       
   212     /**
       
   213      *
       
   214      */
       
   215     SERIALIZABLE;
       
   216   }
       
   217 
       
   218   /**
       
   219    *
       
   220    */
       
   221   public enum Holdability {
       
   222 
       
   223     /**
       
   224      *
       
   225      */
       
   226     HOLD_OVER_COMMIT,
       
   227 
       
   228     /**
       
   229      *
       
   230      */
       
   231     CLOSE_AT_COMMIT;
       
   232   }
       
   233 
       
   234 }