comparison src/main/java/org/monetdb/jdbc/MonetDataSource.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/jdbc/MonetDataSource.java@54137aeb1f92
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
7 */
8
9 package org.monetdb.jdbc;
10
11 import java.io.PrintWriter;
12 import java.sql.Connection;
13 import java.sql.SQLException;
14 import java.sql.SQLFeatureNotSupportedException;
15 import javax.sql.DataSource;
16 import java.util.Properties;
17
18 /**
19 * A DataSource suitable for the MonetDB database.
20 *
21 * This DataSource allows retrieval of a Connection using the JNDI bean like
22 * framework. A DataSource has numerous advantages over using the DriverManager
23 * to retrieve a Connection object. Using the DataSource interface enables a
24 * more transparent application where the location or database can be changed
25 * without changing any application code.
26 *
27 * Additionally, pooled connections can be used when using a DataSource.
28 *
29 * @author Fabian Groffen
30 * @version 0.2
31 */
32 public final class MonetDataSource
33 extends MonetWrapper
34 implements DataSource
35 {
36 private String description;
37 private int loginTimeout = 0;
38 private String user;
39 // insecure, but how to do it better?
40 private String password;
41 private String url;
42
43 // the following properties are also standard:
44 // private String dataSourceName;
45 // private String networkProtocol;
46 // private String serverName;
47 // private String role;
48
49
50 private final MonetDriver driver;
51
52 /**
53 * Constructor of a MonetDataSource which uses default settings for a
54 * connection. You probably want to change this setting using the
55 * method setURL.
56 */
57 public MonetDataSource() {
58 description = "MonetDB database";
59 url = "jdbc:monetdb://localhost/";
60
61 driver = new MonetDriver();
62 }
63
64 /**
65 * Attempts to establish a connection with the data source that this
66 * DataSource object represents.
67 *
68 * @return a MonetConnection
69 * @throws SQLException if connecting to the database fails
70 */
71 @Override
72 public Connection getConnection() throws SQLException {
73 return getConnection(user, password);
74 }
75
76 /**
77 * Attempts to establish a connection with the data source that this
78 * DataSource object represents.
79 *
80 * @param username the username to use
81 * @param password the password to use
82 * @return a MonetConnection
83 * @throws SQLException if connecting to the database fails
84 */
85 @Override
86 public Connection getConnection(final String username, final String password)
87 throws SQLException
88 {
89 if (loginTimeout > 0) {
90 /// could enable Socket.setSoTimeout(int timeout) here...
91 }
92 final Properties props = new Properties();
93 props.put("user", username);
94 props.put("password", password);
95
96 return driver.connect(url, props);
97 }
98
99
100 /**
101 * Gets the maximum time in seconds that this data source can wait while
102 * attempting to connect to a database.
103 *
104 * @return login timeout default is 0 (infinite)
105 */
106 @Override
107 public int getLoginTimeout() {
108 return loginTimeout;
109 }
110
111 /**
112 * Sets the maximum time in seconds that this data source will wait while
113 * attempting to connect to a database.
114 *
115 * @param seconds the number of seconds to wait before aborting the connect
116 */
117 @Override
118 public void setLoginTimeout(final int seconds) {
119 loginTimeout = seconds;
120 }
121
122 /**
123 * Retrieves the log writer for this DataSource object.
124 *
125 * @return null, since there is no log writer
126 */
127 @Override
128 public PrintWriter getLogWriter() {
129 return null;
130 }
131
132 /**
133 * Sets the log writer for this DataSource object to the given
134 * java.io.PrintWriter object.
135 *
136 * @param out a PrintWriter - ignored
137 */
138 @Override
139 public void setLogWriter(final PrintWriter out) {
140 }
141
142 /**
143 * Sets the password to use when connecting. There is no getter
144 * for obvious reasons.
145 *
146 * @param password the password
147 */
148 public void setPassword(final String password) {
149 this.password = password;
150 }
151
152 /**
153 * Gets the username
154 *
155 * @return the username
156 */
157 public String getUser() {
158 return user;
159 }
160
161 /**
162 * Sets the username
163 *
164 * @param user the username
165 */
166 public void setUser(final String user) {
167 this.user = user;
168 }
169
170 /**
171 * Gets the connection URL
172 *
173 * @return the connection URL
174 */
175 public String getURL() {
176 return url;
177 }
178
179 /**
180 * Sets the connection URL
181 *
182 * @param url the connection URL
183 */
184 public void setDatabaseName(final String url) {
185 this.url = url;
186 }
187
188 /**
189 * Gets the description
190 *
191 * @return the description
192 */
193 public String getDescription() {
194 return description;
195 }
196
197 /**
198 * Sets the description
199 *
200 * @param description the description
201 */
202 public void setDescription(final String description) {
203 this.description = description;
204 }
205
206 /**
207 * Return the parent Logger of all the Loggers used by this data
208 * source. This should be the Logger farthest from the root Logger
209 * that is still an ancestor of all of the Loggers used by this data
210 * source. Configuring this Logger will affect all of the log
211 * messages generated by the data source. In the worst case, this
212 * may be the root Logger.
213 *
214 * @return the parent Logger for this data source
215 * @throws SQLFeatureNotSupportedException if the data source does
216 * not use java.util.logging
217 */
218 @Override
219 public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
220 throw newSQLFeatureNotSupportedException("getParentLogger");
221 }
222 }