Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 407:40a1db14dca5
Optimise code, reduce duplicate code, cache major and minor version nrs of connected database server and max nr of clients so conversion of strings to ints is only done once.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 07 Jan 2021 19:15:51 +0100 (2021-01-07) |
parents | bf9f6b6ecf40 |
children | 5540793628d6 |
comparison
equal
deleted
inserted
replaced
406:bf9f6b6ecf40 | 407:40a1db14dca5 |
---|---|
74 private final Properties conn_props = new Properties(); | 74 private final Properties conn_props = new Properties(); |
75 | 75 |
76 /** The hostname to connect to */ | 76 /** The hostname to connect to */ |
77 private final String hostname; | 77 private final String hostname; |
78 /** The port to connect on the host to */ | 78 /** The port to connect on the host to */ |
79 private int port = 0; | 79 private int port; |
80 /** The database to use (currently not used) */ | 80 /** The database to use (currently not used) */ |
81 private final String database; | 81 private final String database; |
82 /** The username to use when authenticating */ | 82 /** The username to use when authenticating */ |
83 private final String username; | 83 private final String username; |
84 /** The password to use when authenticating */ | 84 /** The password to use when authenticating */ |
99 | 99 |
100 /** Whether this Connection is in autocommit mode */ | 100 /** Whether this Connection is in autocommit mode */ |
101 private boolean autoCommit = true; | 101 private boolean autoCommit = true; |
102 | 102 |
103 /** The stack of warnings for this Connection object */ | 103 /** The stack of warnings for this Connection object */ |
104 private SQLWarning warnings = null; | 104 private SQLWarning warnings; |
105 | 105 |
106 /** The Connection specific mapping of user defined types to Java types */ | 106 /** The Connection specific mapping of user defined types to Java types */ |
107 private Map<String,Class<?>> typeMap = new HashMap<String,Class<?>>() { | 107 private Map<String,Class<?>> typeMap = new HashMap<String,Class<?>>() { |
108 private static final long serialVersionUID = 1L; | 108 private static final long serialVersionUID = 1L; |
109 { | 109 { |
1012 */ | 1012 */ |
1013 @Override | 1013 @Override |
1014 public void setAutoCommit(final boolean autoCommit) throws SQLException { | 1014 public void setAutoCommit(final boolean autoCommit) throws SQLException { |
1015 checkNotClosed(); | 1015 checkNotClosed(); |
1016 if (this.autoCommit != autoCommit) { | 1016 if (this.autoCommit != autoCommit) { |
1017 sendControlCommand("auto_commit " + (autoCommit ? "1" : "0")); | 1017 sendControlCommand(autoCommit ? "auto_commit 1" : "auto_commit 0"); |
1018 this.autoCommit = autoCommit; | 1018 this.autoCommit = autoCommit; |
1019 } | 1019 } |
1020 } | 1020 } |
1021 | 1021 |
1022 /** | 1022 /** |
1072 * @throws SQLException if a database access error occurs or this Connection | 1072 * @throws SQLException if a database access error occurs or this Connection |
1073 * object is currently in auto-commit mode | 1073 * object is currently in auto-commit mode |
1074 */ | 1074 */ |
1075 @Override | 1075 @Override |
1076 public Savepoint setSavepoint() throws SQLException { | 1076 public Savepoint setSavepoint() throws SQLException { |
1077 checkNotClosed(); | 1077 return setSavepoint(null); |
1078 // create a new Savepoint object | |
1079 final MonetSavepoint sp = new MonetSavepoint(); | |
1080 | |
1081 // note: can't use sendIndependentCommand here because we need | |
1082 // to process the auto_commit state the server gives | |
1083 sendTransactionCommand("SAVEPOINT " + sp.getName()); | |
1084 return sp; | |
1085 } | 1078 } |
1086 | 1079 |
1087 /** | 1080 /** |
1088 * Creates a savepoint with the given name in the current transaction | 1081 * Creates a savepoint with the given name in the current transaction |
1089 * and returns the new Savepoint object that represents it. | 1082 * and returns the new Savepoint object that represents it. |
1097 public Savepoint setSavepoint(final String name) throws SQLException { | 1090 public Savepoint setSavepoint(final String name) throws SQLException { |
1098 checkNotClosed(); | 1091 checkNotClosed(); |
1099 // create a new Savepoint object | 1092 // create a new Savepoint object |
1100 final MonetSavepoint sp; | 1093 final MonetSavepoint sp; |
1101 try { | 1094 try { |
1102 sp = new MonetSavepoint(name); | 1095 sp = (name != null) ? new MonetSavepoint(name) : new MonetSavepoint(); |
1103 } catch (IllegalArgumentException e) { | 1096 } catch (IllegalArgumentException e) { |
1104 throw new SQLException(e.getMessage(), "M0M03"); | 1097 throw new SQLException(e.getMessage(), "M0M03"); |
1105 } | 1098 } |
1106 | 1099 |
1107 // note: can't use sendIndependentCommand here because we need | 1100 // note: can't use sendIndependentCommand here because we need |
1700 ret = ret.replaceAll("'", "\\\\'"); | 1693 ret = ret.replaceAll("'", "\\\\'"); |
1701 return ret; | 1694 return ret; |
1702 } | 1695 } |
1703 | 1696 |
1704 | 1697 |
1705 // Internal cache for 3 static mserver environment values, so they aren't queried from mserver again and again | 1698 // Internal caches for 3 static mserver environment values, so they aren't queried from mserver again and again |
1706 private String env_current_user = null; | 1699 private String env_current_user; |
1707 private String env_monet_version = null; | 1700 private String env_monet_version; |
1708 private String env_max_clients = null; | 1701 private int maxConnections; |
1702 private int databaseMajorVersion; | |
1703 private int databaseMinorVersion; | |
1709 | 1704 |
1710 /** | 1705 /** |
1711 * Utility method to fetch 3 mserver environment values combined in one query for efficiency. | 1706 * Utility method to fetch 3 mserver environment values combined in one query for efficiency. |
1712 * We currently fetch the env values of: current_user, monet_version and max_clients. | 1707 * We currently fetch the env values of: current_user, monet_version and max_clients. |
1713 * We cache them such that we do not need to query the server again and again. | 1708 * We cache them such that we do not need to query the server again and again. |
1730 env_current_user = value; | 1725 env_current_user = value; |
1731 } else | 1726 } else |
1732 if ("monet_version".equals(prop)) { | 1727 if ("monet_version".equals(prop)) { |
1733 env_monet_version = value; | 1728 env_monet_version = value; |
1734 } else | 1729 } else |
1735 if ("max_clients".equals(prop)) { | 1730 if ("max_clients".equals(prop) && value != null) { |
1736 env_max_clients = value; | 1731 try { |
1732 maxConnections = Integer.parseInt(value); | |
1733 } catch (NumberFormatException nfe) { | |
1734 /* ignore */ | |
1735 } | |
1736 if (maxConnections <= 0) | |
1737 maxConnections = 1; | |
1737 } | 1738 } |
1738 } | 1739 } |
1739 } | 1740 } |
1740 } | 1741 } |
1741 /* do not catch SQLException here, as we need to know it when it fails */ | 1742 /* do not catch SQLException here, as we need to know it when it fails */ |
1742 } finally { | 1743 } finally { |
1743 closeResultsetStatement(rs, st); | 1744 closeResultsetStatement(rs, st); |
1744 } | 1745 } |
1745 // for debug: System.out.println("Read: env_current_user: " + env_current_user + " env_monet_version: " + env_monet_version + " env_max_clients: " + env_max_clients); | 1746 // for debug: System.out.println("Read: env_current_user: " + env_current_user + " env_monet_version: " + env_monet_version + " env_max_clients: " + maxConnections); |
1746 } | 1747 } |
1747 | 1748 |
1748 /** | 1749 /** |
1749 * @return the current User Name. | 1750 * @return the current User Name. |
1750 * It is called from: MonetDatabaseMetaData | 1751 * It is called from: MonetDatabaseMetaData |
1771 /** | 1772 /** |
1772 * @return the MonetDB Database Server major version number. | 1773 * @return the MonetDB Database Server major version number. |
1773 * It is called from: MonetDatabaseMetaData | 1774 * It is called from: MonetDatabaseMetaData |
1774 */ | 1775 */ |
1775 int getDatabaseMajorVersion() throws SQLException { | 1776 int getDatabaseMajorVersion() throws SQLException { |
1776 if (env_monet_version == null) | 1777 if (databaseMajorVersion == 0) { |
1777 getEnvValues(); | 1778 if (env_monet_version == null) |
1778 if (env_monet_version != null) { | 1779 getEnvValues(); |
1779 try { | 1780 if (env_monet_version != null) { |
1780 // from version string such as 11.33.9 extract number: 11 | 1781 try { |
1781 final int start = env_monet_version.indexOf('.'); | 1782 // from version string such as 11.33.9 extract number: 11 |
1782 return Integer.parseInt((start >= 0) ? env_monet_version.substring(0, start) : env_monet_version); | 1783 final int start = env_monet_version.indexOf('.'); |
1783 } catch (NumberFormatException nfe) { | 1784 databaseMajorVersion = Integer.parseInt((start >= 0) ? env_monet_version.substring(0, start) : env_monet_version); |
1784 // ignore | 1785 } catch (NumberFormatException nfe) { |
1786 // ignore | |
1787 } | |
1785 } | 1788 } |
1786 } | 1789 } |
1787 return 0; | 1790 return databaseMajorVersion; |
1788 } | 1791 } |
1789 | 1792 |
1790 /** | 1793 /** |
1791 * @return the MonetDB Database Server minor version number. | 1794 * @return the MonetDB Database Server minor version number. |
1792 * It is called from: MonetDatabaseMetaData | 1795 * It is called from: MonetDatabaseMetaData |
1793 */ | 1796 */ |
1794 int getDatabaseMinorVersion() throws SQLException { | 1797 int getDatabaseMinorVersion() throws SQLException { |
1795 if (env_monet_version == null) | 1798 if (databaseMinorVersion == 0) { |
1796 getEnvValues(); | 1799 if (env_monet_version == null) |
1797 if (env_monet_version != null) { | 1800 getEnvValues(); |
1798 try { | 1801 if (env_monet_version != null) { |
1799 // from version string such as 11.33.9 extract number: 33 | 1802 try { |
1800 int start = env_monet_version.indexOf('.'); | 1803 // from version string such as 11.33.9 extract number: 33 |
1801 if (start >= 0) { | 1804 int start = env_monet_version.indexOf('.'); |
1802 start++; | 1805 if (start >= 0) { |
1803 final int end = env_monet_version.indexOf('.', start); | 1806 start++; |
1804 return Integer.parseInt((end > 0) ? env_monet_version.substring(start, end) : env_monet_version.substring(start)); | 1807 final int end = env_monet_version.indexOf('.', start); |
1808 databaseMinorVersion = Integer.parseInt((end > 0) ? env_monet_version.substring(start, end) : env_monet_version.substring(start)); | |
1809 } | |
1810 } catch (NumberFormatException nfe) { | |
1811 // ignore | |
1805 } | 1812 } |
1806 } catch (NumberFormatException nfe) { | |
1807 // ignore | |
1808 } | 1813 } |
1809 } | 1814 } |
1810 return 0; | 1815 return databaseMinorVersion; |
1811 } | 1816 } |
1812 | 1817 |
1813 /** | 1818 /** |
1814 * @return the maximum number of active connections possible at one time; | 1819 * @return the maximum number of active connections possible at one time; |
1815 * a result of zero means that there is no limit or the limit is not known | 1820 * a result of zero means that there is no limit or the limit is not known |
1816 * It is called from: MonetDatabaseMetaData | 1821 * It is called from: MonetDatabaseMetaData |
1817 */ | 1822 */ |
1818 int getMaxConnections() throws SQLException { | 1823 int getMaxConnections() throws SQLException { |
1819 if (env_max_clients == null) | 1824 if (maxConnections == 0) |
1820 getEnvValues(); | 1825 getEnvValues(); |
1821 if (env_max_clients != null) { | 1826 return maxConnections; |
1822 try { | |
1823 return Integer.parseInt(env_max_clients); | |
1824 } catch (NumberFormatException nfe) { | |
1825 /* ignore */ | |
1826 } | |
1827 } | |
1828 return 0; | |
1829 } | 1827 } |
1830 | 1828 |
1831 | 1829 |
1832 // Internal cache for determining if system table sys.privilege_codes (new as of Jul2017 release) exists on connected server | 1830 // Internal cache for determining if system table sys.privilege_codes (new as of Jul2017 release) exists on connected server |
1833 private boolean queriedPrivilege_codesTable = false; | 1831 private boolean queriedPrivilege_codesTable = false; |