Skip to content

Commit 0dbcad0

Browse files
add various methods
cloneConnection, createTemporaryOrPrivateConnection, closeConnection, abortConnection, getSchema, getUser, isSupported
1 parent fb6fe36 commit 0dbcad0

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

sqldev/src/main/java/org/utplsql/sqldev/model/DatabaseTools.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
import javax.sql.DataSource;
2323

24+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
2425
import org.utplsql.sqldev.exception.GenericDatabaseAccessException;
26+
import org.utplsql.sqldev.exception.GenericRuntimeException;
2527

2628
import oracle.dbtools.raptor.navigator.db.DatabaseConnection;
2729
import oracle.dbtools.raptor.utils.Connections;
2830
import oracle.javatools.db.DBException;
31+
import oracle.jdeveloper.db.ConnectionException;
2932

3033
public class DatabaseTools {
3134
// do not instantiate this class
@@ -59,11 +62,112 @@ public static Connection getConnection(String connectionName) {
5962
}
6063
}
6164

65+
public static Connection cloneConnection(String connectionName) {
66+
final Connection conn = getConnection(connectionName);
67+
try {
68+
return Connections.getInstance().cloneConnection(conn);
69+
} catch (ConnectionException e) {
70+
final String msg = "Error cloning connection " + connectionName + ".";
71+
throw new GenericDatabaseAccessException(msg, e);
72+
}
73+
}
74+
75+
private static String createTemporaryConnection(String connectionName) {
76+
try {
77+
return Connections.getInstance().createTemporaryConnection(connectionName);
78+
} catch (Throwable e) {
79+
final String msg = "Error creating temporary connection based on " + connectionName + ".";
80+
throw new GenericDatabaseAccessException(msg, e);
81+
}
82+
}
83+
84+
private static String createPrivateConnection(String connectionName) {
85+
try {
86+
return Connections.getInstance().createPrivateConnection(connectionName);
87+
} catch (Throwable e) {
88+
final String msg = "Error creating private connection based on " + connectionName + ".";
89+
throw new GenericDatabaseAccessException(msg, e);
90+
}
91+
}
92+
93+
public static String createTemporaryOrPrivateConnection(String connectionName) {
94+
// Private connections are closed in SQL Developer < 17.4.0 when the worksheet
95+
// is closed, but in SQL Developer > 17.4.0 private connections are not closed.
96+
// Temporary connections have been introduced in SQL Developer 17.4.0. They will
97+
// be always closed, when a worksheet is closed.
98+
// Hence we try to use temporary connections whenever possible. See also
99+
// https://github.com/utPLSQL/utPLSQL-SQLDeveloper/issues/47 .
100+
try {
101+
return createTemporaryConnection(connectionName);
102+
} catch (GenericDatabaseAccessException e) {
103+
return createPrivateConnection(connectionName);
104+
}
105+
}
106+
62107
public static boolean isConnectionClosed(Connection conn) {
63108
try {
64109
return conn.isClosed();
65110
} catch (SQLException e) {
66111
throw new GenericDatabaseAccessException("Error getting status of connection.", e);
67112
}
68113
}
114+
115+
public static void closeConnection(Connection conn) {
116+
if (!isConnectionClosed(conn)) {
117+
try {
118+
conn.close();
119+
} catch (SQLException e) {
120+
throw new GenericDatabaseAccessException("Could not close connection.");
121+
}
122+
}
123+
}
124+
125+
public static void abortConnection(Connection conn) {
126+
final SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
127+
try {
128+
conn.abort(taskExecutor);
129+
} catch (SQLException e) {
130+
throw new GenericDatabaseAccessException("Could not abort connection.");
131+
}
132+
}
133+
134+
public static String getSchema(Connection conn) {
135+
try {
136+
return conn.getSchema();
137+
} catch (SQLException e) {
138+
throw new GenericRuntimeException("Error getting schema name of connection.", e);
139+
}
140+
}
141+
142+
public static String getUser(Connection conn) {
143+
try {
144+
return conn.getMetaData().getUserName();
145+
} catch (SQLException e) {
146+
throw new GenericRuntimeException("Error getting user name of connection.", e);
147+
}
148+
}
149+
150+
public static String getSchema(DatabaseConnection conn) {
151+
return getSchema(getConnection(conn));
152+
}
153+
154+
155+
public static String getSchema(String connectionName) {
156+
return getSchema(getConnection(connectionName));
157+
}
158+
159+
public static boolean isSupported(final Connection conn) {
160+
try {
161+
boolean ret = false;
162+
if (conn != null && conn.getMetaData().getDatabaseProductName().startsWith("Oracle")
163+
&& (conn.getMetaData().getDatabaseMajorVersion() == 11
164+
&& conn.getMetaData().getDatabaseMinorVersion() >= 2
165+
|| conn.getMetaData().getDatabaseMajorVersion() > 11)) {
166+
ret = true;
167+
}
168+
return ret;
169+
} catch (SQLException e) {
170+
throw new GenericDatabaseAccessException("Error while getting product version of connection.", e);
171+
}
172+
}
69173
}

0 commit comments

Comments
 (0)