Skip to content

Commit 479692e

Browse files
add DatabaseTools.java to wrap getConnection() calls
1 parent 5491052 commit 479692e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2020 Philipp Salvisberg <philipp.salvisberg@trivadis.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.utplsql.sqldev.model;
17+
18+
import java.io.IOException;
19+
import java.sql.Connection;
20+
import java.sql.SQLException;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.utplsql.sqldev.exception.GenericDatabaseAccessException;
25+
26+
import oracle.dbtools.raptor.navigator.db.DatabaseConnection;
27+
import oracle.dbtools.raptor.utils.Connections;
28+
import oracle.javatools.db.DBException;
29+
30+
public class DatabaseTools {
31+
// do not instantiate this class
32+
private DatabaseTools() {
33+
super();
34+
}
35+
36+
public static Connection getConnection(DataSource dataSource) {
37+
try {
38+
return dataSource.getConnection();
39+
} catch (SQLException e) {
40+
throw new GenericDatabaseAccessException("Error getting connection.", e);
41+
}
42+
}
43+
44+
public static Connection getConnection(DatabaseConnection conn) {
45+
try {
46+
return conn.getConnection();
47+
} catch (IOException e) {
48+
final String msg = "Error getting connection for " + conn.getConnectionName() + ".";
49+
throw new GenericDatabaseAccessException(msg, e);
50+
}
51+
}
52+
53+
public static Connection getConnection(String connectionName) {
54+
try {
55+
return Connections.getInstance().getConnection(connectionName);
56+
} catch (DBException e) {
57+
final String msg = "Error getting connection for " + connectionName + ".";
58+
throw new GenericDatabaseAccessException(msg, e);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)