| 1 | /* |
| 2 | * Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package jakarta.annotation.sql; |
| 18 | |
| 19 | import java.lang.annotation.*; |
| 20 | import java.lang.annotation.Target; |
| 21 | import java.lang.annotation.Retention; |
| 22 | import java.lang.annotation.ElementType; |
| 23 | import java.lang.annotation.RetentionPolicy; |
| 24 | |
| 25 | /** |
| 26 | * Annotation used to define a container <code>DataSource</code> to |
| 27 | * be registered with JNDI. The <code>DataSource</code> may be configured by |
| 28 | * setting the annotation elements for commonly used <code>DataSource</code> |
| 29 | * properties. Additional standard and vendor-specific properties may be |
| 30 | * specified using the <code>properties</code> element. |
| 31 | * <p> |
| 32 | * |
| 33 | * The data source will be registered under the name specified in the |
| 34 | * <code>name</code> element. It may be defined to be in any valid |
| 35 | * Jakarta EE namespace, which will determine the accessibility of |
| 36 | * the data source from other components. |
| 37 | * <p> |
| 38 | * A JDBC driver implementation class of the appropriate type, either |
| 39 | * <code>DataSource</code>, <code>ConnectionPoolDataSource</code>, or |
| 40 | * <code>XADataSource</code>, must be indicated by the <code>className</code> |
| 41 | * element. The availability of the driver class will be assumed at runtime. |
| 42 | *<p> |
| 43 | * DataSource properties should not be specified more than once. If |
| 44 | * the url annotation element contains a DataSource property that was also |
| 45 | * specified using the corresponding annotation element or was specified in |
| 46 | * the properties annotation element, the precedence order is undefined |
| 47 | * and implementation specific: |
| 48 | * <p> |
| 49 | * <pre> |
| 50 | * @DataSourceDefinition(name="java:global/MyApp/MyDataSource", |
| 51 | * className="org.apache.derby.jdbc.ClientDataSource", |
| 52 | * url="jdbc:derby://localhost:1527/myDB;user=bill", |
| 53 | * user="lance", |
| 54 | * password="secret", |
| 55 | * databaseName="testDB", |
| 56 | * serverName="luckydog" |
| 57 | * )// DO NOT DO THIS!!! |
| 58 | * </pre> |
| 59 | * <p> |
| 60 | * In the above example, the <code>databaseName</code>, <code>user</code> |
| 61 | * and <code>serverName</code> properties were specified as part of the |
| 62 | * <code>url</code> property and using the corresponding |
| 63 | * annotation elements. This should be avoided. |
| 64 | * <p> |
| 65 | * If the <code>properties</code> annotation element is used and contains a |
| 66 | * DataSource property that was also specified using the corresponding |
| 67 | * annotation element, the annotation element value takes precedence. |
| 68 | * For example: |
| 69 | * <p> |
| 70 | * <pre> |
| 71 | * @DataSourceDefinition(name="java:global/MyApp/MyDataSource", |
| 72 | * className="org.apache.derby.jdbc.ClientDataSource", |
| 73 | * user="lance", |
| 74 | * password="secret", |
| 75 | * databaseName="testDB", |
| 76 | * serverName="luckydog", |
| 77 | * properties= {"databaseName=myDB", "databaseProp=doThis"} |
| 78 | * )// DO NOT DO THIS!!! |
| 79 | * </pre> |
| 80 | * <p> |
| 81 | * This would result in the following values being used when configuring |
| 82 | * the DataSource: |
| 83 | * <ul> |
| 84 | * <li>serverName=luckydog</li> |
| 85 | * <li>portNumber=1527</li> |
| 86 | * <li>databaseName=testDB</li> |
| 87 | * <li>user=lance</li> |
| 88 | * <li>password=secret</li> |
| 89 | * <li>databaseProp=doThis</li> |
| 90 | * </ul> |
| 91 | * <p> |
| 92 | * Vendors are not required to support properties that do not normally |
| 93 | * apply to a specific data source type. For example, specifying the |
| 94 | * <code>transactional</code> property to be <code>true</code> but supplying |
| 95 | * a value for <code>className</code> that implements a data source class |
| 96 | * other than <code>XADataSource</code> may not be supported. |
| 97 | * <p> |
| 98 | * Vendor-specific properties may be combined with or used to |
| 99 | * override standard data source properties defined using this annotation. |
| 100 | * <p> |
| 101 | * <code>DataSource</code> properties that are specified and are not supported |
| 102 | * in a given configuration or cannot be mapped to a vendor specific |
| 103 | * configuration property may be ignored. |
| 104 | * <p> |
| 105 | * Examples: |
| 106 | * <br> |
| 107 | * <pre> |
| 108 | * @DataSourceDefinition(name="java:global/MyApp/MyDataSource", |
| 109 | * className="com.foobar.MyDataSource", |
| 110 | * portNumber=6689, |
| 111 | * serverName="myserver.com", |
| 112 | * user="lance", |
| 113 | * password="secret" |
| 114 | * ) |
| 115 | * |
| 116 | * </pre> |
| 117 | * <p> |
| 118 | * Using a <code>URL</code>: |
| 119 | * <br> |
| 120 | * <pre> |
| 121 | * @DataSourceDefinition(name="java:global/MyApp/MyDataSource", |
| 122 | * className="org.apache.derby.jdbc.ClientDataSource", |
| 123 | * url="jdbc:derby://localhost:1527/myDB", |
| 124 | * user="lance", |
| 125 | * password="secret" |
| 126 | * ) |
| 127 | * </pre> |
| 128 | * <p> |
| 129 | * An example lookup of the DataSource from an Jakarta Enterprise Beans: |
| 130 | * <pre> |
| 131 | * @Stateless |
| 132 | * public class MyStatelessEJB { |
| 133 | * @Resource(lookup="java:global/MyApp/myDataSource") |
| 134 | * DataSource myDB; |
| 135 | * ... |
| 136 | * } |
| 137 | * </pre> |
| 138 | * <p> |
| 139 | * @see javax.sql.DataSource |
| 140 | * @see javax.sql.XADataSource |
| 141 | * @see javax.sql.ConnectionPoolDataSource |
| 142 | * @since Common Annotations 1.1 |
| 143 | */ |
| 144 | @Target({ElementType.TYPE}) |
| 145 | @Retention(RetentionPolicy.RUNTIME) |
| 146 | @Repeatable(DataSourceDefinitions.class) |
| 147 | public @interface DataSourceDefinition { |
| 148 | |
| 149 | /** |
| 150 | * JNDI name by which the data source will be registered. |
| 151 | * @since 1.1 |
| 152 | */ |
| 153 | String name(); |
| 154 | |
| 155 | /** |
| 156 | * Name of a DataSource class that implements |
| 157 | * <code>javax.sql.DataSource</code> or <code>javax.sql.XADataSource</code> |
| 158 | * or <code>javax.sql.ConnectionPoolDataSource</code>. |
| 159 | * @since 1.1 |
| 160 | */ |
| 161 | String className(); |
| 162 | |
| 163 | /** |
| 164 | * Description of this data source |
| 165 | * @since 1.1 |
| 166 | */ |
| 167 | String description() default ""; |
| 168 | |
| 169 | /** |
| 170 | * A JDBC URL. If the <code>url</code> annotation element contains a |
| 171 | * DataSource property that was also specified using the corresponding |
| 172 | * annotation element, the precedence order is undefined and |
| 173 | * implementation specific. |
| 174 | * @since 1.1 |
| 175 | */ |
| 176 | String url() default ""; |
| 177 | |
| 178 | /** |
| 179 | * User name to use for connection authentication. |
| 180 | * @since 1.1 |
| 181 | */ |
| 182 | String user() default ""; |
| 183 | |
| 184 | /** |
| 185 | * Password to use for connection authentication. |
| 186 | * @since 1.1 |
| 187 | */ |
| 188 | String password() default ""; |
| 189 | |
| 190 | /** |
| 191 | * Name of a database on a server. |
| 192 | * @since 1.1 |
| 193 | */ |
| 194 | String databaseName() default ""; |
| 195 | |
| 196 | /** |
| 197 | * Port number where a server is listening for requests. |
| 198 | * @since 1.1 |
| 199 | */ |
| 200 | int portNumber() default -1; |
| 201 | |
| 202 | /** |
| 203 | * Database server name. |
| 204 | * @since 1.1 |
| 205 | */ |
| 206 | String serverName() default "localhost"; |
| 207 | |
| 208 | /** |
| 209 | * Isolation level for connections. The Isolation level |
| 210 | * must be one of the following: |
| 211 | * <p> |
| 212 | * <ul> |
| 213 | * <li>Connection.TRANSACTION_NONE, |
| 214 | * <li>Connection.TRANSACTION_READ_ UNCOMMITTED, |
| 215 | * <li>Connection.TRANSACTION_READ_COMMITTED, |
| 216 | * <li>Connection.TRANSACTION_REPEATABLE_READ, |
| 217 | * <li>Connection.TRANSACTION_SERIALIZABLE |
| 218 | *</ul> |
| 219 | * <p> |
| 220 | * Default is vendor-specific. |
| 221 | * @since 1.1 |
| 222 | */ |
| 223 | int isolationLevel() default -1; |
| 224 | |
| 225 | /** |
| 226 | * Set to <code>false</code> if connections should not participate |
| 227 | * in transactions. |
| 228 | * <p> |
| 229 | * Default is to enlist in a transaction when one is active or becomes |
| 230 | * active. |
| 231 | * @since 1.1 |
| 232 | */ |
| 233 | boolean transactional() default true; |
| 234 | |
| 235 | /** |
| 236 | * Number of connections that should be created when a connection pool |
| 237 | * is initialized. |
| 238 | * <p> |
| 239 | * Default is vendor-specific |
| 240 | * @since 1.1 |
| 241 | */ |
| 242 | int initialPoolSize() default -1; |
| 243 | |
| 244 | /** |
| 245 | * Maximum number of connections that should be concurrently allocated for a |
| 246 | * connection pool. |
| 247 | * <p> |
| 248 | * Default is vendor-specific. |
| 249 | * @since 1.1 |
| 250 | */ |
| 251 | int maxPoolSize() default -1; |
| 252 | |
| 253 | /** |
| 254 | * Minimum number of connections that should be allocated for a |
| 255 | * connection pool. |
| 256 | * <p> |
| 257 | * Default is vendor-specific. |
| 258 | * @since 1.1 |
| 259 | */ |
| 260 | int minPoolSize() default -1; |
| 261 | |
| 262 | /** |
| 263 | * The number of seconds that a physical connection |
| 264 | * should remain unused in the pool before the |
| 265 | * connection is closed for a connection pool. |
| 266 | * <p> |
| 267 | * Default is vendor-specific |
| 268 | * @since 1.1 |
| 269 | */ |
| 270 | int maxIdleTime() default -1; |
| 271 | |
| 272 | /** |
| 273 | * The total number of statements that a connection pool should keep open. |
| 274 | * A value of 0 indicates that the caching of statements is disabled for |
| 275 | * a connection pool. |
| 276 | * <p> |
| 277 | * Default is vendor-specific |
| 278 | * @since 1.1 |
| 279 | */ |
| 280 | int maxStatements() default -1; |
| 281 | /** |
| 282 | * Used to specify vendor-specific properties and less commonly |
| 283 | * used <code>DataSource</code> properties such as: |
| 284 | * <p> |
| 285 | * <ul> |
| 286 | * <li>dataSourceName |
| 287 | * <li>networkProtocol |
| 288 | * <li>propertyCycle |
| 289 | * <li>roleName |
| 290 | * </ul> |
| 291 | * <p> |
| 292 | * Properties are specified using the format: |
| 293 | * <i>propertyName=propertyValue</i> with one property per array element. |
| 294 | * <p> |
| 295 | * If a DataSource property is specified in the <code>properties</code> |
| 296 | * element and the annotation element for the property is also |
| 297 | * specified, the annotation element value takes precedence. |
| 298 | * @since 1.1 |
| 299 | */ |
| 300 | String[] properties() default {}; |
| 301 | |
| 302 | |
| 303 | /** |
| 304 | * Sets the maximum time in seconds that this data source will wait while |
| 305 | * attempting to connect to a database. A value of zero specifies that |
| 306 | * the timeout is the default system timeout if there is one; otherwise, |
| 307 | * it specifies that there is no timeout. |
| 308 | * <p> |
| 309 | * Default is vendor-specific. |
| 310 | * @since 1.1 |
| 311 | */ |
| 312 | int loginTimeout() default 0; |
| 313 | } |