KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > ClientDataSource


1 /*
2
3    Derby - Class org.apache.derby.client.ClientDataSource
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21
22 package org.apache.derby.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27
28 import org.apache.derby.client.am.LogWriter;
29 import org.apache.derby.client.am.SqlException;
30 import org.apache.derby.client.net.NetConnection;
31 import org.apache.derby.client.net.NetLogWriter;
32
33 /**
34  * ClientDataSource is a simple data source implementation
35  * that can be used for establishing connections in a
36  * non-pooling, non-distributed environment.
37  * The class ClientConnectionPoolDataSource can be used in a connection pooling environment,
38  * and the class ClientXADataSource can be used in a distributed, and pooling
39  * environment. Use these DataSources if your application runs under
40  * JDBC3.0 or JDBC2.0, that is, on the following Java Virtual Machines:
41  * <p/>
42  * <UL>
43  * <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
44  * <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
45  * </UL>
46  *
47  * <p>The example below registers a DNC data source object with a JNDI naming service.
48  * <pre>
49  * org.apache.derby.client.ClientDataSource dataSource = new org.apache.derby.client.ClientDataSource ();
50  * dataSource.setServerName ("my_derby_database_server");
51  * dataSource.setDatabaseName ("my_derby_database_name");
52  * javax.naming.Context context = new javax.naming.InitialContext();
53  * context.bind ("jdbc/my_datasource_name", dataSource);
54  * </pre>
55  * The first line of code in the example creates a data source object.
56  * The next two lines initialize the data source's
57  * properties. Then a Java object that references the initial JNDI naming
58  * context is created by calling the
59  * InitialContext() constructor, which is provided by JNDI.
60  * System properties (not shown) are used to tell JNDI the
61  * service provider to use. The JNDI name space is hierarchical,
62  * similar to the directory structure of many file
63  * systems. The data source object is bound to a logical JNDI name
64  * by calling Context.bind(). In this case the JNDI name
65  * identifies a subcontext, "jdbc", of the root naming context
66  * and a logical name, "my_datasource_name", within the jdbc
67  * subcontext. This is all of the code required to deploy
68  * a data source object within JNDI. This example is provided
69  * mainly for illustrative purposes. We expect that developers
70  * or system administrators will normally use a GUI tool to
71  * deploy a data source object.
72  * <p/>
73  * Once a data source has been registered with JNDI,
74  * it can then be used by a JDBC application, as is shown in the
75  * following example.
76  * <pre>
77  * javax.naming.Context context = new javax.naming.InitialContext ();
78  * javax.sql.DataSource dataSource = (javax.sql.DataSource) context.lookup ("jdbc/my_datasource_name");
79  * java.sql.Connection connection = dataSource.getConnection ("user", "password");
80  * </pre>
81  * The first line in the example creates a Java object
82  * that references the initial JNDI naming context. Next, the
83  * initial naming context is used to do a lookup operation
84  * using the logical name of the data source. The
85  * Context.lookup() method returns a reference to a Java Object,
86  * which is narrowed to a javax.sql.DataSource object. In
87  * the last line, the DataSource.getConnection() method
88  * is called to produce a database connection.
89  * <p/>
90  * This simple data source subclass of ClientBaseDataSource maintains
91  * it's own private <code>password</code> property.
92  * <p/>
93  * The specified password, along with the user, is validated by DERBY.
94  * This property can be overwritten by specifing
95  * the password parameter on the DataSource.getConnection() method call.
96  * <p/>
97  * This password property is not declared transient, and therefore
98  * may be serialized to a file in clear-text, or stored
99  * to a JNDI server in clear-text when the data source is saved.
100  * Care must taken by the user to prevent security
101  * breaches.
102  * <p/>
103  */

104 public class ClientDataSource extends ClientBaseDataSource implements DataSource JavaDoc {
105     private final static long serialVersionUID = 1894299584216955553L;
106     public static final String JavaDoc className__ = "org.apache.derby.jdbc.ClientDataSource";
107
108     // If a newer version of a serialized object has to be compatible with an older version, it is important that the newer version abides
109
// by the rules for compatible and incompatible changes.
110
//
111
// A compatible change is one that can be made to a new version of the class, which still keeps the stream compatible with older
112
// versions of the class. Examples of compatible changes are:
113
//
114
// Addition of new fields or classes does not affect serialization, as any new data in the stream is simply ignored by older
115
// versions. When the instance of an older version of the class is deserialized, the newly added field will be set to its default
116
// value.
117
// You can field change access modifiers like private, public, protected or package as they are not reflected to the serial
118
// stream.
119
// You can change a transient or static field to a non-transient or non-static field, as it is similar to adding a field.
120
// You can change the access modifiers for constructors and methods of the class. For instance a previously private method
121
// can now be made public, an instance method can be changed to static, etc. The only exception is that you cannot change
122
// the default signatures for readObject() and writeObject() if you are implementing custom serialization. The serialization
123
// process looks at only instance data, and not the methods of a class.
124
//
125
// Changes which would render the stream incompatible are:
126
//
127
// Once a class implements the Serializable interface, you cannot later make it implement the Externalizable interface, since
128
// this will result in the creation of an incompatible stream.
129
// Deleting fields can cause a problem. Now, when the object is serialized, an earlier version of the class would set the old
130
// field to its default value since nothing was available within the stream. Consequently, this default data may lead the newly
131
// created object to assume an invalid state.
132
// Changing a non-static into static or non-transient into transient is not permitted as it is equivalent to deleting fields.
133
// You also cannot change the field types within a class, as this would cause a failure when attempting to read in the original
134
// field into the new field.
135
// You cannot alter the position of the class in the class hierarchy. Since the fully-qualified class name is written as part of
136
// the bytestream, this change will result in the creation of an incompatible stream.
137
// You cannot change the name of the class or the package it belongs to, as that information is written to the stream during
138
// serialization.
139

140
141     /**
142      * Creates a simple DERBY data source with default property values for a non-pooling, non-distributed environment.
143      * No particular DatabaseName or other properties are associated with the data source.
144      * <p/>
145      * Every Java Bean should provide a constructor with no arguments since many beanboxes attempt to instantiate a bean
146      * by invoking its no-argument constructor.
147      */

148     public ClientDataSource() {
149         super();
150     }
151
152
153     // ---------------------------interface methods-------------------------------
154

155     /**
156      * Attempt to establish a database connection in a non-pooling, non-distributed environment.
157      *
158      * @return a Connection to the database
159      *
160      * @throws java.sql.SQLException if a database-access error occurs.
161      */

162     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
163         return getConnection(getUser(), getPassword());
164     }
165
166     /**
167      * Attempt to establish a database connection in a non-pooling, non-distributed environment.
168      *
169      * @param user the database user on whose behalf the Connection is being made
170      * @param password the user's password
171      *
172      * @return a Connection to the database
173      *
174      * @throws java.sql.SQLException if a database-access error occurs.
175      */

176     public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
177         // Jdbc 2 connections will write driver trace info on a
178
// datasource-wide basis using the jdbc 2 data source log writer.
179
// This log writer may be narrowed to the connection-level
180
// This log writer will be passed to the agent constructor.
181

182         try
183         {
184             LogWriter dncLogWriter = super.computeDncLogWriterForNewConnection("_sds");
185             updateDataSourceValues(tokenizeAttributes(getConnectionAttributes(), null));
186             return ClientDriver.getFactory().newNetConnection
187                     ((NetLogWriter) dncLogWriter, user,
188                     password, this, -1, false);
189         }
190         catch(SqlException se)
191         {
192             throw se.getSQLException();
193         }
194         
195     }
196
197 }
198
199
Popular Tags