Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @ 348:bcb9a99d399d
Add private checkNotClosed() method to check if the connection is not closed.
Call the method from all methods which require that the connection is not closed.
Disabled not needed check in setNetworkTimeout(), such that it can be used now.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 21 Nov 2019 19:58:18 +0100 (2019-11-21) |
parents | 45155894aece |
children | 54137aeb1f92 |
comparison
equal
deleted
inserted
replaced
347:4e2685f0b7fd | 348:bcb9a99d399d |
---|---|
236 | 236 |
237 // check mandatory input arguments | 237 // check mandatory input arguments |
238 if (hostname == null || hostname.isEmpty()) | 238 if (hostname == null || hostname.isEmpty()) |
239 throw new IllegalArgumentException("Missing or empty host name"); | 239 throw new IllegalArgumentException("Missing or empty host name"); |
240 if (port <= 0 || port > 65535) | 240 if (port <= 0 || port > 65535) |
241 throw new IllegalArgumentException("Invalid port number: " + port + ". It should not be " + (port < 0 ? "negative" : (port > 65535 ? "larger than 65535" : "0"))); | 241 throw new IllegalArgumentException("Invalid port number: " + port |
242 + ". It should not be " + (port < 0 ? "negative" : (port > 65535 ? "larger than 65535" : "0"))); | |
242 if (username == null || username.isEmpty()) | 243 if (username == null || username.isEmpty()) |
243 throw new IllegalArgumentException("Missing or empty user name"); | 244 throw new IllegalArgumentException("Missing or empty user name"); |
244 if (password == null || password.isEmpty()) | 245 if (password == null || password.isEmpty()) |
245 throw new IllegalArgumentException("Missing or empty password"); | 246 throw new IllegalArgumentException("Missing or empty password"); |
246 if (language == null || language.isEmpty()) { | 247 if (language == null || language.isEmpty()) { |
582 * @throws SQLException if a database access error occurs or this method is | 583 * @throws SQLException if a database access error occurs or this method is |
583 * called on a closed connection | 584 * called on a closed connection |
584 */ | 585 */ |
585 @Override | 586 @Override |
586 public SQLWarning getWarnings() throws SQLException { | 587 public SQLWarning getWarnings() throws SQLException { |
587 if (closed) | 588 checkNotClosed(); |
588 throw new SQLException("Cannot call on closed Connection", "M1M20"); | |
589 | |
590 // if there are no warnings, this will be null, which fits with the | 589 // if there are no warnings, this will be null, which fits with the |
591 // specification. | 590 // specification. |
592 return warnings; | 591 return warnings; |
593 } | 592 } |
594 | 593 |
698 */ | 697 */ |
699 @Override | 698 @Override |
700 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) | 699 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) |
701 throws SQLException | 700 throws SQLException |
702 { | 701 { |
702 checkNotClosed(); | |
703 try { | 703 try { |
704 final CallableStatement ret = new MonetCallableStatement( | 704 final CallableStatement ret = new MonetCallableStatement( |
705 this, | 705 this, |
706 resultSetType, | 706 resultSetType, |
707 resultSetConcurrency, | 707 resultSetConcurrency, |
802 */ | 802 */ |
803 @Override | 803 @Override |
804 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) | 804 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) |
805 throws SQLException | 805 throws SQLException |
806 { | 806 { |
807 checkNotClosed(); | |
807 try { | 808 try { |
808 final PreparedStatement ret = new MonetPreparedStatement( | 809 final PreparedStatement ret = new MonetPreparedStatement( |
809 this, | 810 this, |
810 resultSetType, | 811 resultSetType, |
811 resultSetConcurrency, | 812 resultSetConcurrency, |
932 * Savepoint object is not a valid savepoint in the current | 933 * Savepoint object is not a valid savepoint in the current |
933 * transaction | 934 * transaction |
934 */ | 935 */ |
935 @Override | 936 @Override |
936 public void releaseSavepoint(final Savepoint savepoint) throws SQLException { | 937 public void releaseSavepoint(final Savepoint savepoint) throws SQLException { |
938 checkNotClosed(); | |
937 if (!(savepoint instanceof MonetSavepoint)) | 939 if (!(savepoint instanceof MonetSavepoint)) |
938 throw new SQLException("This driver can only handle savepoints it created itself", "M0M06"); | 940 throw new SQLException("This driver can only handle savepoints it created itself", "M0M06"); |
939 | 941 |
940 final MonetSavepoint sp = (MonetSavepoint)savepoint; | 942 final MonetSavepoint sp = (MonetSavepoint)savepoint; |
941 | 943 |
953 * Connection object is in auto-commit mode | 955 * Connection object is in auto-commit mode |
954 * @see #setAutoCommit(boolean) | 956 * @see #setAutoCommit(boolean) |
955 */ | 957 */ |
956 @Override | 958 @Override |
957 public void rollback() throws SQLException { | 959 public void rollback() throws SQLException { |
960 checkNotClosed(); | |
958 // note: can't use sendIndependentCommand here because we need | 961 // note: can't use sendIndependentCommand here because we need |
959 // to process the auto_commit state the server gives | 962 // to process the auto_commit state the server gives |
960 sendTransactionCommand("ROLLBACK"); | 963 sendTransactionCommand("ROLLBACK"); |
961 } | 964 } |
962 | 965 |
970 * Savepoint object is no longer valid, or this Connection | 973 * Savepoint object is no longer valid, or this Connection |
971 * object is currently in auto-commit mode | 974 * object is currently in auto-commit mode |
972 */ | 975 */ |
973 @Override | 976 @Override |
974 public void rollback(final Savepoint savepoint) throws SQLException { | 977 public void rollback(final Savepoint savepoint) throws SQLException { |
978 checkNotClosed(); | |
975 if (!(savepoint instanceof MonetSavepoint)) | 979 if (!(savepoint instanceof MonetSavepoint)) |
976 throw new SQLException("This driver can only handle savepoints it created itself", "M0M06"); | 980 throw new SQLException("This driver can only handle savepoints it created itself", "M0M06"); |
977 | 981 |
978 final MonetSavepoint sp = (MonetSavepoint)savepoint; | 982 final MonetSavepoint sp = (MonetSavepoint)savepoint; |
979 | 983 |
1006 * @throws SQLException if a database access error occurs | 1010 * @throws SQLException if a database access error occurs |
1007 * @see #getAutoCommit() | 1011 * @see #getAutoCommit() |
1008 */ | 1012 */ |
1009 @Override | 1013 @Override |
1010 public void setAutoCommit(final boolean autoCommit) throws SQLException { | 1014 public void setAutoCommit(final boolean autoCommit) throws SQLException { |
1015 checkNotClosed(); | |
1011 if (this.autoCommit != autoCommit) { | 1016 if (this.autoCommit != autoCommit) { |
1012 sendControlCommand("auto_commit " + (autoCommit ? "1" : "0")); | 1017 sendControlCommand("auto_commit " + (autoCommit ? "1" : "0")); |
1013 this.autoCommit = autoCommit; | 1018 this.autoCommit = autoCommit; |
1014 } | 1019 } |
1015 } | 1020 } |
1067 * @throws SQLException if a database access error occurs or this Connection | 1072 * @throws SQLException if a database access error occurs or this Connection |
1068 * object is currently in auto-commit mode | 1073 * object is currently in auto-commit mode |
1069 */ | 1074 */ |
1070 @Override | 1075 @Override |
1071 public Savepoint setSavepoint() throws SQLException { | 1076 public Savepoint setSavepoint() throws SQLException { |
1077 checkNotClosed(); | |
1072 // create a new Savepoint object | 1078 // create a new Savepoint object |
1073 final MonetSavepoint sp = new MonetSavepoint(); | 1079 final MonetSavepoint sp = new MonetSavepoint(); |
1074 | 1080 |
1075 // note: can't use sendIndependentCommand here because we need | 1081 // note: can't use sendIndependentCommand here because we need |
1076 // to process the auto_commit state the server gives | 1082 // to process the auto_commit state the server gives |
1087 * @throws SQLException if a database access error occurs or this Connection | 1093 * @throws SQLException if a database access error occurs or this Connection |
1088 * object is currently in auto-commit mode | 1094 * object is currently in auto-commit mode |
1089 */ | 1095 */ |
1090 @Override | 1096 @Override |
1091 public Savepoint setSavepoint(final String name) throws SQLException { | 1097 public Savepoint setSavepoint(final String name) throws SQLException { |
1098 checkNotClosed(); | |
1092 // create a new Savepoint object | 1099 // create a new Savepoint object |
1093 final MonetSavepoint sp; | 1100 final MonetSavepoint sp; |
1094 try { | 1101 try { |
1095 sp = new MonetSavepoint(name); | 1102 sp = new MonetSavepoint(name); |
1096 } catch (IllegalArgumentException e) { | 1103 } catch (IllegalArgumentException e) { |
1359 */ | 1366 */ |
1360 @Override | 1367 @Override |
1361 public String getClientInfo(final String name) throws SQLException { | 1368 public String getClientInfo(final String name) throws SQLException { |
1362 if (name == null || name.isEmpty()) | 1369 if (name == null || name.isEmpty()) |
1363 return null; | 1370 return null; |
1371 checkNotClosed(); | |
1364 return conn_props.getProperty(name); | 1372 return conn_props.getProperty(name); |
1365 } | 1373 } |
1366 | 1374 |
1367 /** | 1375 /** |
1368 * Returns a list containing the name and current value of each client info | 1376 * Returns a list containing the name and current value of each client info |
1376 * or this method is called on a closed connection | 1384 * or this method is called on a closed connection |
1377 * @since 1.6 | 1385 * @since 1.6 |
1378 */ | 1386 */ |
1379 @Override | 1387 @Override |
1380 public Properties getClientInfo() throws SQLException { | 1388 public Properties getClientInfo() throws SQLException { |
1389 checkNotClosed(); | |
1381 // return a clone of the connection properties object | 1390 // return a clone of the connection properties object |
1382 return new Properties(conn_props); | 1391 return new Properties(conn_props); |
1383 } | 1392 } |
1384 | 1393 |
1385 /** | 1394 /** |
1492 * method is called on a closed connection | 1501 * method is called on a closed connection |
1493 * @since 1.7 | 1502 * @since 1.7 |
1494 */ | 1503 */ |
1495 @Override | 1504 @Override |
1496 public void setSchema(final String schema) throws SQLException { | 1505 public void setSchema(final String schema) throws SQLException { |
1497 if (closed) | 1506 checkNotClosed(); |
1498 throw new SQLException("Cannot call on closed Connection", "M1M20"); | |
1499 if (schema == null || schema.isEmpty()) | 1507 if (schema == null || schema.isEmpty()) |
1500 throw new SQLException("Missing schema name", "M1M05"); | 1508 throw new SQLException("Missing schema name", "M1M05"); |
1501 | 1509 |
1502 Statement st = null; | 1510 Statement st = null; |
1503 try { | 1511 try { |
1518 * method is called on a closed connection | 1526 * method is called on a closed connection |
1519 * @since 1.7 | 1527 * @since 1.7 |
1520 */ | 1528 */ |
1521 @Override | 1529 @Override |
1522 public String getSchema() throws SQLException { | 1530 public String getSchema() throws SQLException { |
1523 if (closed) | 1531 checkNotClosed(); |
1524 throw new SQLException("Cannot call on closed Connection", "M1M20"); | |
1525 | 1532 |
1526 String cur_schema = null; | 1533 String cur_schema = null; |
1527 Statement st = null; | 1534 Statement st = null; |
1528 ResultSet rs = null; | 1535 ResultSet rs = null; |
1529 try { | 1536 try { |
1593 * null, or the value specified for seconds is less than 0. | 1600 * null, or the value specified for seconds is less than 0. |
1594 * @since 1.7 | 1601 * @since 1.7 |
1595 */ | 1602 */ |
1596 @Override | 1603 @Override |
1597 public void setNetworkTimeout(final Executor executor, final int millis) throws SQLException { | 1604 public void setNetworkTimeout(final Executor executor, final int millis) throws SQLException { |
1598 if (closed) | 1605 checkNotClosed(); |
1599 throw new SQLException("Cannot call on closed Connection", "M1M20"); | 1606 // executor object is not used yet, so no need to test it. |
1600 if (executor == null) | 1607 // if (executor == null) |
1601 throw new SQLException("executor is null", "M1M05"); | 1608 // throw new SQLException("executor is null", "M1M05"); |
1602 if (millis < 0) | 1609 if (millis < 0) |
1603 throw new SQLException("milliseconds is less than zero", "M1M05"); | 1610 throw new SQLException("milliseconds is less than zero", "M1M05"); |
1604 | 1611 |
1605 try { | 1612 try { |
1606 server.setSoTimeout(millis); | 1613 server.setSoTimeout(millis); |
1620 * this method is called on a closed Connection | 1627 * this method is called on a closed Connection |
1621 * @since 1.7 | 1628 * @since 1.7 |
1622 */ | 1629 */ |
1623 @Override | 1630 @Override |
1624 public int getNetworkTimeout() throws SQLException { | 1631 public int getNetworkTimeout() throws SQLException { |
1625 if (closed) | 1632 checkNotClosed(); |
1626 throw new SQLException("Cannot call on closed Connection", "M1M20"); | |
1627 | |
1628 try { | 1633 try { |
1629 return server.getSoTimeout(); | 1634 return server.getSoTimeout(); |
1630 } catch (SocketException e) { | 1635 } catch (SocketException e) { |
1631 throw new SQLNonTransientConnectionException(e.getMessage(), "08000"); | 1636 throw new SQLNonTransientConnectionException(e.getMessage(), "08000"); |
1632 } | 1637 } |
1651 * instead of getClob() and Clob.getCharacterStream() to reduce overhead. | 1656 * instead of getClob() and Clob.getCharacterStream() to reduce overhead. |
1652 * It is called from: MonetResultSet and MonetPreparedStatement | 1657 * It is called from: MonetResultSet and MonetPreparedStatement |
1653 */ | 1658 */ |
1654 boolean mapClobAsVarChar() { | 1659 boolean mapClobAsVarChar() { |
1655 return treatClobAsVarChar; | 1660 return treatClobAsVarChar; |
1661 } | |
1662 | |
1663 /** | |
1664 * Local helper method to test whether the Connection object is closed | |
1665 * When closed it throws an SQLException | |
1666 */ | |
1667 private void checkNotClosed() throws SQLException { | |
1668 if (closed) | |
1669 throw new SQLException("Connection is closed", "M1M20"); | |
1656 } | 1670 } |
1657 | 1671 |
1658 /** | 1672 /** |
1659 * @return the MonetDB JDBC Connection URL (without user name and password). | 1673 * @return the MonetDB JDBC Connection URL (without user name and password). |
1660 * It is called from: getURL()in MonetDatabaseMetaData | 1674 * It is called from: getURL()in MonetDatabaseMetaData |