H2 Database Study Notes interaction between jdbc and TcpServer

Source: Internet
Author: User

This article will share with you an example about the interaction between H2 Database and TcpServer.

This section mainly records the main process of TcpServer. H2 jdbc accesses TcpServer. The main process is TcpServer.

The listen () method of the TcpServer listens to new connection requests. When a new connection is received, the TcpServerThread object is created, representing a connection. The Session and Transfer objects in the connection correspond to each other.

The process method of TcpServerThread is the main execution method. A simple select request will interact with the client seven times.

1 SESSION_SET_ID: Obtain the session id passed by the client.

2 SESSION_PREPARE_READ_PARAMS CREATE Command

Session. prepareLocal (SQL) mainly used for SQL Parsing

Parser. parseSelectSimpleFromPart (Select)

-> ReadTableFilter: Find the table used and create the tableFilter according to the table.

Returns CommandContainer for jobs before SQL Execution.

Select init () prepare ()

3 COMMAND_EXECUTE_QUERY status: Call the query method of CommandContainer,

ResultInterface result = prepared. query (maxrows); run the SQL Execution Plan and obtain the result. Adjusted to Query. query

For example, Select. queryWithoutCache

4 RESULT_CLOSE release the result object in the cache

5 COMMAND_CLOSE release the comand object

6 SESSION_PREPARE_READ_PARAMS and COMMAND_EXECUTE_UPDATE ROLLBACK roll back the current transaction and clear the temporary table

7 SESSION_CLOSE close the session and stop the TcpServerThread

Relationship with H2 JDBC
When the client establishes a connection with tcpServer through H2 jdbc, the server jumps to SESSION_SET_ID to obtain the session id.

When the client executes executeQuery, The SESSION_PREPARE_READ_PARAMS and COMMAND_EXECUTE_QUERY

The client conn. close () corresponds to SESSION_CLOSE

The same conn executes the executeQuery, which corresponds to RESULT_CLOSE and COMMAND_CLOSE to release resources.

It can be seen that there are seven interactions between request creation, query execution, and request closure and client. If you reuse an existing connection, perform steps 2, 3, 4, and 5 for each query.

SESSION_PREPARE_READ_PARAMS and COMMAND_EXECUTE_UPDATE will be interacted twice each time the select command is executed, which is a waste of resources.

 

Object relationship
TcpServerThread -- 1 to 1 -- Transfer, Session

Session --- 1 to many --- Command --- 1 to 1 --- Prepared --- 1 to 1 --- Parser

Each client connects to TcpServerThread and corresponds to a session id.

Jdbc source code mainly depends on JdbcConnection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.