Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
SimpleDataSource.java
Go to the documentation of this file.
1package dev.rafex.ether.jdbc.datasource;
2
3/*-
4 * #%L
5 * ether-jdbc
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.io.PrintWriter;
30import java.sql.Connection;
31import java.sql.DriverManager;
32import java.sql.SQLException;
33import java.sql.SQLFeatureNotSupportedException;
34import java.util.Objects;
35import java.util.Properties;
36import java.util.logging.Logger;
37
38import javax.sql.DataSource;
39
40/**
41 * A simple implementation of {@link DataSource} that uses {@link DriverManager} to create connections.
42 *
43 * <p>This implementation is suitable for applications that need a straightforward way to obtain
44 * database connections without complex connection pooling.</p>
45 */
46public final class SimpleDataSource implements DataSource {
47
48 private final String url;
49 private final Properties properties;
50 private volatile PrintWriter logWriter;
51 private volatile int loginTimeout;
52
53 /**
54 * Creates a new {@code SimpleDataSource} with the specified JDBC URL.
55 *
56 * @param url the JDBC URL of the database
57 * @throws NullPointerException if {@code url} is {@code null}
58 */
59 public SimpleDataSource(final String url) {
60 this(url, new Properties());
61 }
62
63 /**
64 * Creates a new {@code SimpleDataSource} with the specified JDBC URL, username, and password.
65 *
66 * @param url the JDBC URL of the database
67 * @param username the database username
68 * @param password the database password
69 * @throws NullPointerException if {@code url} is {@code null}
70 */
71 public SimpleDataSource(final String url, final String username, final String password) {
72 this(url, toProperties(username, password));
73 }
74
75 /**
76 * Creates a new {@code SimpleDataSource} with the specified JDBC URL and properties.
77 *
78 * @param url the JDBC URL of the database
79 * @param properties the connection properties
80 * @throws NullPointerException if {@code url} is {@code null}
81 */
82 public SimpleDataSource(final String url, final Properties properties) {
83 this.url = Objects.requireNonNull(url, "url");
84 this.properties = new Properties();
85 if (properties != null) {
86 this.properties.putAll(properties);
87 }
88 }
89
90 /**
91 * Retrieves a database connection using the configured URL and properties.
92 *
93 * @return a database connection
94 * @throws SQLException if a database access error occurs
95 */
96 @Override
97 public Connection getConnection() throws SQLException {
98 DriverManager.setLoginTimeout(loginTimeout);
99 if (logWriter != null) {
100 DriverManager.setLogWriter(logWriter);
101 }
102 return DriverManager.getConnection(url, properties);
103 }
104
105 /**
106 * Retrieves a database connection using the specified username and password.
107 *
108 * @param username the database username
109 * @param password the database password
110 * @return a database connection
111 * @throws SQLException if a database access error occurs
112 */
113 @Override
114 public Connection getConnection(final String username, final String password) throws SQLException {
115 return DriverManager.getConnection(url, username, password);
116 }
117
118 @Override
119 public PrintWriter getLogWriter() {
120 return logWriter;
121 }
122
123 @Override
124 public void setLogWriter(final PrintWriter out) {
125 this.logWriter = out;
126 }
127
128 @Override
129 public void setLoginTimeout(final int seconds) {
130 this.loginTimeout = seconds;
131 }
132
133 @Override
134 public int getLoginTimeout() {
135 return loginTimeout;
136 }
137
138 /**
139 * Not supported by this implementation.
140 *
141 * @return nothing, always throws an exception
142 * @throws SQLFeatureNotSupportedException always
143 */
144 @Override
145 public Logger getParentLogger() throws SQLFeatureNotSupportedException {
146 throw new SQLFeatureNotSupportedException("Parent logger is not supported");
147 }
148
149 @Override
150 public <T> T unwrap(final Class<T> iface) throws SQLException {
151 if (iface.isInstance(this)) {
152 return iface.cast(this);
153 }
154 throw new SQLException("Unsupported unwrap type: " + iface.getName());
155 }
156
157 @Override
158 public boolean isWrapperFor(final Class<?> iface) {
159 return iface.isInstance(this);
160 }
161
162 private static Properties toProperties(final String username, final String password) {
163 final var properties = new Properties();
164 if (username != null) {
165 properties.setProperty("user", username);
166 }
167 if (password != null) {
168 properties.setProperty("password", password);
169 }
170 return properties;
171 }
172}
Logger getParentLogger()
Not supported by this implementation.
Connection getConnection(final String username, final String password)
Retrieves a database connection using the specified username and password.
SimpleDataSource(final String url, final String username, final String password)
Creates a new SimpleDataSource with the specified JDBC URL, username, and password.
SimpleDataSource(final String url)
Creates a new SimpleDataSource with the specified JDBC URL.
Connection getConnection()
Retrieves a database connection using the configured URL and properties.
SimpleDataSource(final String url, final Properties properties)
Creates a new SimpleDataSource with the specified JDBC URL and properties.