| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>com.mckoi.database.jdbcserver - Server and embedded-database classes</title> |
| 4 |
</head> |
| 5 |
<body> |
| 6 |
<p> |
| 7 |
Implementation of the DatabaseInterface interface, |
| 8 |
including classes to handle local (embedded) mode |
| 9 |
and remote (client/server) mode. |
| 10 |
<p> |
| 11 |
See <a href="../jdbc/DatabaseInterface.html">DatabaseInterface</a>. |
| 12 |
|
| 13 |
<a name="local_mode"> |
| 14 |
<h3>Local (Embedded) mode</h3></a> |
| 15 |
|
| 16 |
When accessing a local database, the client uses an instance of |
| 17 |
<a href="LocalDatabaseInterface.html">LocalDatabaseInterface</a> |
| 18 |
(as set up while |
| 19 |
<a href="../jdbc/package-summary.html#establish_connection"> |
| 20 |
Establishing a Connection</a>) |
| 21 |
to directly access the underlying database. |
| 22 |
See <a href="#local_queries">Local Queries</a>. |
| 23 |
|
| 24 |
<a name="remote_mode"> |
| 25 |
<h3>Remote (Client/Server) mode</h3></a> |
| 26 |
|
| 27 |
When accessing a remote database, |
| 28 |
requests in the client are directed to an instance of |
| 29 |
<a href="JDBCDatabaseInterface.html">JDBCDatabaseInterface</a> |
| 30 |
(as set up while |
| 31 |
<a href="../jdbc/package-summary.html#establish_connection"> |
| 32 |
Establishing a Connection</a>) |
| 33 |
to send the requests over a TCP connection to the database server. |
| 34 |
The server uses an instance of |
| 35 |
<a href="LocalDatabaseInterface.html">LocalDatabaseInterface</a> |
| 36 |
to execute SQL statements in local mode on behalf of the client. |
| 37 |
See <a href="#remote_queries">Remote Queries</a>. |
| 38 |
|
| 39 |
<a name="local_queries"> |
| 40 |
<h3>Local Queries</h3></a> |
| 41 |
|
| 42 |
When an application is accessing a local database, |
| 43 |
or when considering query processing within the database server, |
| 44 |
SQL queries all go through |
| 45 |
<a href="LocalDatabaseInterface.html#execQuery(com.mckoi.database.jdbc.SQLQuery)"> |
| 46 |
LocalDatabaseInterface.execQuery</a>. |
| 47 |
This method uses |
| 48 |
<a href="../DatabaseSystem.html#execute(com.mckoi.database.User, com.mckoi.database.DatabaseConnection, java.lang.Runnable)"> |
| 49 |
com.mckoi.database.DatabaseSystem.execute</a> |
| 50 |
to execute the |
| 51 |
<a href="JDBCDatabaseInterface.html#execQuery(com.mckoi.database.jdbc.SQLQuery)"> |
| 52 |
execQuery</a> method |
| 53 |
(see <a href="#query_execution">Query Execution</a>) |
| 54 |
of the contained |
| 55 |
<a href="JDBCDatabaseInterface.html">JDBCDatabaseInterface</a> |
| 56 |
in the first available |
| 57 |
<a href="../WorkerThread.html">WorkerThread</a>. |
| 58 |
The calling thread waits for the WorkerThread to finish and return a |
| 59 |
<a href="../jdbc/QueryResponse.html">QueryResponse</a>, |
| 60 |
which is used by the caller to set up a |
| 61 |
<a href="../jdbc/MResultSet.html">MResultSet</a>. |
| 62 |
|
| 63 |
<a name="remote_queries"> |
| 64 |
<h3>Remote Queries</h3> |
| 65 |
|
| 66 |
When an application is running in remote mode, |
| 67 |
SQL queries are processed by the private ConnectionThread class within |
| 68 |
<a href="../jdbc/RemoteDatabaseInterface.html"> |
| 69 |
com.mckoi.jdbc.RemoteDatabaseInterface</a>. |
| 70 |
The executeQuery method in this class writes the sql query string to |
| 71 |
the socket attached to the server and returns an integer ID for that |
| 72 |
sql request. |
| 73 |
This is followed by a call to the getCommand method, which blocks until |
| 74 |
it times out or until it gets a reply from the server for the right ID. |
| 75 |
If a reply is received, the first byte is decoded as a status byte. |
| 76 |
On success, an inner-class instance of |
| 77 |
<a href="../jdbc/QueryResponse.html">QueryResponse</a> |
| 78 |
is returned; on error, error info is read from the response and used |
| 79 |
to create and throw an exception. |
| 80 |
|
| 81 |
<p> |
| 82 |
Data is written to the server in the calling thread. |
| 83 |
There is a separate thread |
| 84 |
(an instance of the private class RemoteDatabaseInterface.ConnectionThread in |
| 85 |
<a href="../jdbc/RemoteDatabaseInterface.html">RemoteDatabaseInterface</a>) |
| 86 |
to read the data coming back from the server, |
| 87 |
which is placed into a buffer where it can be accessed by other threads. |
| 88 |
|
| 89 |
<a name="query_execution"> |
| 90 |
<h3>Query Execution</h3></a> |
| 91 |
|
| 92 |
Queries in local mode and queries handled by the database server |
| 93 |
are both processed by |
| 94 |
<a href="JDBCDatabaseInterface.html#execQuery(com.mckoi.database.jdbc.SQLQuery)"> |
| 95 |
JDBCDatabaseInterface.execQuery</a>. |
| 96 |
This method always runs in a |
| 97 |
<a href="../WorkerThread.html">WorkerThread</a>. |
| 98 |
The execQuery method takes these steps to execute the query: |
| 99 |
<ul> |
| 100 |
<li>Set user and connection data into the WorkerThread. |
| 101 |
<li>Output debug log info. |
| 102 |
<li>Record start time. |
| 103 |
<li>Parse sql statement into statement_tree, check cache. |
| 104 |
<li>Bind values for query parameters. |
| 105 |
<li>Prepare all expression in statement tree. |
| 106 |
<li>Convert to instance of some |
| 107 |
<a href="../interpret/Statement.html">Statement</a> subclass |
| 108 |
(such as |
| 109 |
<a href="../interpret/Insert.html">Insert</a> or |
| 110 |
<a href="../interpret/Delete.html">Delete</a>.) |
| 111 |
These are all in the package |
| 112 |
<a href="../interpret/package-summary.html"> |
| 113 |
com.mckoi.database.interpret</a>. |
| 114 |
<li>Set up <a href="../package-summary#locking">locking</a>. |
| 115 |
<li>Finish preparing statement. |
| 116 |
<li>Get list of tables to read and write. |
| 117 |
<li>Lock tables. |
| 118 |
<li>Evaluate the Statement using |
| 119 |
<a href="../interpret/Statement.html#evaluate()">Statement.evaluate</a>. |
| 120 |
This is different for each different type of Statement. |
| 121 |
<li>Place results into result set cache. |
| 122 |
<li>Unlock tables. |
| 123 |
<li>Check autoCommit, do commit or rollback if set. |
| 124 |
<li>Record end time. |
| 125 |
<li>Return |
| 126 |
<a href="../jdbc/QueryResponse.html">QueryResponse</a> |
| 127 |
info (an instance of the private class JDIQueryResponse in |
| 128 |
<a href="JDBCDatabaseInterface.html">JDBCDatabaseInterface</a>). |
| 129 |
</ul> |
| 130 |
|
| 131 |
<a name="service_initialization"> |
| 132 |
<h3>Service Initialization</h3></a> |
| 133 |
|
| 134 |
After the database server has been started |
| 135 |
and initialized its database, it begins listening for client connections. |
| 136 |
This happens in <a href="TCPServer.html#start(java.util.ResourceBundle)"> |
| 137 |
TCPServer.start</a>. |
| 138 |
The server creates a |
| 139 |
<a href="ConnectionPoolServer.html">ConnectionPoolServer</a>, |
| 140 |
either a |
| 141 |
<a href="SingleThreadedConnectionPoolServer.html"> |
| 142 |
SingleThreadedConnectionPoolServer</a> (when running in single-thead mode) or a |
| 143 |
<a href="MultiThreadedConnectionPoolServer.html"> |
| 144 |
MultiThreadedConnectionPoolServer</a> (when running in multi-thread mode). |
| 145 |
The start method starts a listener thread and then returns. |
| 146 |
|
| 147 |
<p> |
| 148 |
The server listener thread sits in a loop blocked waiting for new connections. |
| 149 |
When a new connection is accepted, the server creates a new |
| 150 |
<a href="JDBCDatabaseInterface.html">JDBCDatabaseInterface</a> |
| 151 |
for that connection, wraps it in a |
| 152 |
<a href="TCPJDBCServerConnection.html">TCPJDBCServerConnection</a>, |
| 153 |
and adds that to the |
| 154 |
<a href="ConnectionPoolServer.html">ConnectionPoolServer</a>. |
| 155 |
|
| 156 |
<a name="client_requests"> |
| 157 |
<h3>Client Requests</h3></a> |
| 158 |
|
| 159 |
<p> |
| 160 |
In single-thread mode, |
| 161 |
the SingleThreadedConnectionPoolServer handles all client reads |
| 162 |
in a single thread. |
| 163 |
For each connection with a request, it calls |
| 164 |
<a href="TCPJDBCServerConnection.html#processRequest()"> |
| 165 |
TCPJDBCServerConnection.processRequest</a> on that connection, |
| 166 |
using |
| 167 |
<a href="../DatabaseSystem.html#execute(com.mckoi.database.User, com.mckoi.database.DatabaseConnection, java.lang.Runnable)"> |
| 168 |
com.mckoi.database.DatabaseSystem.execute</a> |
| 169 |
to execute it in a WorkerThread. |
| 170 |
|
| 171 |
<p> |
| 172 |
In multi-thread mode, |
| 173 |
the MultiThreadedConnectionPoolServer starts a separate thread to handle |
| 174 |
reads for each connection. |
| 175 |
Each thread blocks until it has a request available, then calls |
| 176 |
<a href="TCPJDBCServerConnection.html#processRequest()"> |
| 177 |
TCPJDBCServerConnection.processRequest</a> on that connection, |
| 178 |
using |
| 179 |
<a href="../DatabaseSystem.html#execute(com.mckoi.database.User, com.mckoi.database.DatabaseConnection, java.lang.Runnable)"> |
| 180 |
com.mckoi.database.DatabaseSystem.execute</a> |
| 181 |
to execute it in a WorkerThread. |
| 182 |
|
| 183 |
<p> |
| 184 |
In either single-thread or multi-thread mode, client requests end up getting |
| 185 |
passed to |
| 186 |
<a href="TCPJDBCServerConnection.html#processRequest()"> |
| 187 |
TCPJDBCServerConnection.processRequest</a>. |
| 188 |
This method reads the command data, then calls |
| 189 |
<a href="JDBCProcessor.html#processJDBCCommand(byte[])"> |
| 190 |
JDBCProcessor.processJDBCCommand</a> with those bytes. |
| 191 |
That method calls |
| 192 |
<a href="JDBCProcessor.html#processQuery(byte[])"> |
| 193 |
JDBCProcessor.processQuery</a>, which looks at the first byte of the |
| 194 |
requests to see what it is. |
| 195 |
If the request type is a database query (as opposed to retrieval of |
| 196 |
part of a ResultSet), it creates a |
| 197 |
<a href="../jdbc/SQLQuery.html">SQLQuery</a> from the data and calls |
| 198 |
<a href="JDBCDatabaseInterface.html#execQuery(com.mckoi.database.jdbc.SQLQuery)"> |
| 199 |
execQuery</a> on its |
| 200 |
<a href="JDBCDatabaseInterface.html">JDBCDatabaseInterface</a> |
| 201 |
(see <a href="#query_execution">Query Execution</a> above). |
| 202 |
The result is encoded as a |
| 203 |
<a href="../jdbc/QueryResponse.html">QueryResponse</a>, |
| 204 |
which is converted back to bytes and written back to the client. |
| 205 |
Note that this is all running in a |
| 206 |
<a href="../WorkerThread.html">WorkerThread</a>, |
| 207 |
so the call to DatabaseInterface.execQuery is in a WorkerThread, as required. |
| 208 |
|
| 209 |
<p> |
| 210 |
If there is an exception during processing of a client request, |
| 211 |
the message and traceback for the exception are encoded and |
| 212 |
returned to the client, which recognizes the packet as an exception, |
| 213 |
decodes the data on its side, and throws an exception in the client. |
| 214 |
|
| 215 |
</body> |
| 216 |
</html> |