|
21 | 21 |
|
22 | 22 | import javax.sql.DataSource; |
23 | 23 |
|
| 24 | +import org.springframework.core.task.SimpleAsyncTaskExecutor; |
24 | 25 | import org.utplsql.sqldev.exception.GenericDatabaseAccessException; |
| 26 | +import org.utplsql.sqldev.exception.GenericRuntimeException; |
25 | 27 |
|
26 | 28 | import oracle.dbtools.raptor.navigator.db.DatabaseConnection; |
27 | 29 | import oracle.dbtools.raptor.utils.Connections; |
28 | 30 | import oracle.javatools.db.DBException; |
| 31 | +import oracle.jdeveloper.db.ConnectionException; |
29 | 32 |
|
30 | 33 | public class DatabaseTools { |
31 | 34 | // do not instantiate this class |
@@ -59,11 +62,112 @@ public static Connection getConnection(String connectionName) { |
59 | 62 | } |
60 | 63 | } |
61 | 64 |
|
| 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 | + |
62 | 107 | public static boolean isConnectionClosed(Connection conn) { |
63 | 108 | try { |
64 | 109 | return conn.isClosed(); |
65 | 110 | } catch (SQLException e) { |
66 | 111 | throw new GenericDatabaseAccessException("Error getting status of connection.", e); |
67 | 112 | } |
68 | 113 | } |
| 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 | + } |
69 | 173 | } |
0 commit comments