changeset 286:d430f8adbf1b

Add keyword "final" to those variables which do not change after first assigment. In MonetConnection.java corrected size of array (was 7): isSet = new boolean[4]; In MonetPreparedStatement.java removed variable connection as it is already present in superclass MonetStatement (changed it into protected). In MonetResultSet.java sinplified code for assigning Connection conn and DatabaseMetaData dbmd. It is now done on instantiation. This allows them to become "final".
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 25 Jul 2019 19:04:10 +0200 (2019-07-25)
parents 637899bda602
children 2ad7f42f141d f412032e3b43
files src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
diffstat 5 files changed, 14 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java
@@ -1933,13 +1933,11 @@ public class MonetConnection
 		/** A List of result blocks (chunks of size fetchSize/cacheSize) */
 		private DataBlockResponse[] resultBlocks;
 
-		/** A bitmap telling whether the headers are set or not */
-		private boolean[] isSet;
 		/** Whether this Response is closed */
 		private boolean closed;
 
 		/** The Connection that we should use when requesting a new block */
-		private MonetConnection.ResponseList parent;
+		private final MonetConnection.ResponseList parent;
 		/** Whether the fetchSize was explitly set by the user */
 		private boolean cacheSizeSetExplicitly = false;
 		/** Whether we should send an Xclose command to the server
@@ -1951,6 +1949,8 @@ public class MonetConnection
 		/** A parser for header lines */
 		HeaderLineParser hlp;
 
+		/** A boolean array telling whether the headers are set or not */
+		private final boolean[] isSet;
 		private static final int NAMES	= 0;
 		private static final int TYPES	= 1;
 		private static final int TABLES	= 2;
@@ -1978,7 +1978,7 @@ public class MonetConnection
 				int seq)
 			throws SQLException
 		{
-			isSet = new boolean[7];
+			isSet = new boolean[4];
 			this.parent = parent;
 			if (parent.cachesize == 0) {
 				/* Below we have to calculate how many "chunks" we need
@@ -2008,15 +2008,11 @@ public class MonetConnection
 			this.id = id;
 			this.tuplecount = tuplecount;
 			this.columncount = columncount;
-			this.resultBlocks =
-				new DataBlockResponse[(tuplecount / cacheSize) + 1];
+			this.resultBlocks = new DataBlockResponse[(tuplecount / cacheSize) + 1];
 
 			hlp = new HeaderLineParser(columncount);
 
-			resultBlocks[0] = new DataBlockResponse(
-				rowcount,
-				parent.rstype == ResultSet.TYPE_FORWARD_ONLY
-			);
+			resultBlocks[0] = new DataBlockResponse(rowcount, parent.rstype == ResultSet.TYPE_FORWARD_ONLY);
 		}
 
 		/**
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java
@@ -26,7 +26,7 @@ import java.util.ArrayList;
  * @version 0.7
  */
 public class MonetDatabaseMetaData extends MonetWrapper implements DatabaseMetaData {
-	private Connection con;
+	private final Connection con;
 
 	// Internal cache for 3 server environment values
 	private String env_current_user;
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetPreparedStatement.java
@@ -81,8 +81,6 @@ public class MonetPreparedStatement
 
 	private final String[] values;
 
-	private final MonetConnection connection;
-
 	/* placeholders for date/time pattern formats created once (only when needed), used multiple times */
 	/** Format of a timestamp with RFC822 time zone */
 	private SimpleDateFormat mTimestampZ;
@@ -141,8 +139,6 @@ public class MonetPreparedStatement
 		column = new String[size];
 		values = new String[size];
 
-		this.connection = connection;
-
 		// fill the arrays
 		ResultSet rs = super.getResultSet();
 		if (rs != null) {
@@ -218,8 +214,6 @@ public class MonetPreparedStatement
 		id = -1;
 		size = -1;
 		rscolcnt = -1;
-
-		this.connection = connection;
 	}
 	*/
 
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java
@@ -1258,7 +1258,7 @@ public class MonetResultSet
 	 * @return the description of this ResultSet object's columns
 	 */
 	@Override
-	public ResultSetMetaData getMetaData() {
+	public ResultSetMetaData getMetaData() throws SQLException {
 		// return inner class which implements the ResultSetMetaData interface
 		return new rsmdw() {
 			// for the more expensive methods (getPrecision(), getScale(), isNullable()), we provide a simple cache
@@ -1269,8 +1269,8 @@ public class MonetResultSet
 			private final int[] _scale		= new int[array_size];
 			private final int[] _isNullable	= new int[array_size];
 			private final boolean[] _isAutoincrement = new boolean[array_size];
-			private Connection conn = null;
-			private DatabaseMetaData dbmd = null;
+			private final Connection conn = getStatement().getConnection();
+			private final DatabaseMetaData dbmd = conn.getMetaData();
 
 			/**
 			 * A private utility method to check validity of column index number
@@ -1304,14 +1304,6 @@ public class MonetResultSet
 					if (tblName != null && !tblName.isEmpty()) {
 						String colName = getColumnName(column);
 						if (colName != null && !colName.isEmpty()) {
-							if (conn == null) {
-								// first time, get a Connection object and cache it for all next columns
-								conn = getStatement().getConnection();
-							}
-							if (conn != null && dbmd == null) {
-								// first time, get a MetaData object and cache it for all next columns
-								dbmd = conn.getMetaData();
-							}
 							if (dbmd != null) {
 								// for precision, scale, isNullable and isAutoincrement we query the information from data dictionary
 								ResultSet colInfo = dbmd.getColumns(null, schName, tblName, colName);
@@ -1730,10 +1722,6 @@ public class MonetResultSet
 			@Override
 			public String getColumnClassName(int column) throws SQLException {
 				checkColumnIndexValidity(column);
-				if (conn == null) {
-					// first time, get a Connection object and cache it for all next columns
-					conn = getStatement().getConnection();
-				}
 				try {
 					String MonetDBType = types[column - 1];
 					Class<?> type = null;
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
+++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java
@@ -53,7 +53,7 @@ public class MonetStatement
 	static final int DEF_MAXROWS = 0;
 
 	/** The parental Connection object */
-	private MonetConnection connection;
+	protected final MonetConnection connection;
 	/** The last ResponseList object this Statement produced */
 	private MonetConnection.ResponseList lastResponseList;
 	/** The last Response that this object uses */
@@ -522,7 +522,9 @@ public class MonetStatement
 			/* do not catch SQLException here, as we want to know it when it fails */
 			finally {
 				if (st != null) {
-					 st.close();
+					try {
+						 st.close();
+					} catch (SQLException e) { /* ignore */ }
 				}
 			}
 		}