jdbc learns two transactions and data sources

affairs

1. The command line for a transaction is as follows:

start transaction: start a transaction

rollback: rollback

commit : commit the transaction

2. Transaction-related methods in JDBC :

a) Connection.setAutoCommit(boolean b): Set the default transaction to false and start a custom transaction

b) Connection.rollback(): Data rollback.

c) Connection.rollback(Savepoint sp): rollback to a rollback point

d) Connection.commit(): commit the transaction

3. Transaction characteristics:

a) A: Atomicity. Explain that a transaction is an inseparable unit

b) C: Consistency. A transaction must move the database from one consistent state to another consistent state

c) I: Isolation: A transaction cannot be disturbed by other transactions.

d) D : Persistence, once a transaction is committed, it should be saved forever.

4. Errors that can occur with transactions: If the isolation level of a transaction is not considered, the following "incorrect" situations can occur:

a) Dirty read: refers to a transaction reading uncommitted data in another transaction

b)  Non-repeatable read: For a record, the data of the same record is different before and after

c)  Virtual reading ( phantom reading ) : For a table, the number of records read before and after is different.

5. The statement that controls the transaction isolation level in MySql :

a) View the current transaction isolation level: select @@tx_isolation;

b) Set the isolation level (four types): set transaction isolation level

1) READ UNCOMMITTED: Dirty reads, non-repeatable reads, and virtual reads may all occur.

2) READ COMMITTED: Dirty reads can be avoided, non-repeatable reads and virtual reads may occur.

3) REPEATABLE READ: It can avoid dirty reading, non-repeatability, and virtual reading may occur.

4) SERIALIZABLE: Can avoid dirty reads, non-repeatability, and virtual reads.

 

data pool

1.  Data pool: A container that stores many Connection objects. Every time a connection needs to be taken out, when it is closed, the taken out Connection object is put back into the container.

2.  Write a standard database connection pool. If you write your own data pool, implementing the javax.sql.DataSource interface is the standard database connection pool (data source). The solution to the problem is to rewrite the Close method in Connection . There are three ways to change the function of a method of a known class ( rewrite the close method of Connection ).

a)  Define a subclass to extend a function of the parent class (it doesn't work here): Because the subclass inherits Connection , and Connection is an interface. If you want to rewrite, you must rewrite all.

b) Use the packaging design pattern to rewrite the functionality of the original class.

c) Use dynamic proxy to rewrite

3. Use of open source data sources:

a) DBCP : Writing steps of DataBase Connection Pool

1)  Required jar: commons-dbcp.jar commons-pool.jar

2)  Copy the DBCP configuration file ( dbcpconfig.properties ) to the build path

3) Get the data source

DataSource BasicDataSourceFactory.createDataSource(props): get the data source

b) C3p0 : is the code name of a robot

1)  Copy the jar package : c3p0.jar

2)  Copy the c3p0 configuration file ( c3p0-config.xml ) to the build path

3)  Get the data source: ComboPoolDataSource cps=new ComboPoolDataSource();

Connection geyConnection()

c)  Benefits: Multiple databases can be connected, DBCP can only connect to one

d) Easier to use.

 

Data metadata acquisition

1. Common methods of DatabaseMetaData object:

a) getURL(): Returns a String object representing the URL of the database.

b) getUserName(): Returns the user name connected to the current database management system.

c) getDatabaseProductName(): Returns the product name of the database.

d) getDatabaseProductVersion(): Returns the version number of the database.

e) getDriverName(): Returns the name of the driver driver.

f) getDriverVersion(): Returns the version number of the driver.

g) isReadOnly(): Returns a boolean value indicating whether the database only allows read operations.

2. ParameterMetaData : The method of PreparedStatement obtains getParameterMeteData

a) getParameterCount() :  Get the number of specified parameters

b) getParameterType(int param) :  Get the sql type of the specified parameter (the driver may not support it)

3. ResultSetMetaData object : obtained by the method of ResultSet.getMetaData()

a) getColumnCount ()  : returns the number of columns of the resultset object

b) getColumnName(int column) : Get the name of the specified column

c) getColumnTypeName(int column) : Get the type of the specified column java.sql.Type

The use of DBUtil framework

1. The DbUtils class provides routine tasks such as closing the connection, loading the JDBC driver, etc. methods are static

a) public static void close(…) throws java.sql.SQLException:关闭

b) public static void closeQuietly( ): This type of method can not only avoid closing when Connection , Statement and ResultSet are NULL , but also hide some SQLEeception thrown in the program

c) public static void commitAndCloseQuietly(Connection conn) : used to commit the connection, then close the connection, and do not throw an SQL exception when closing the connection .

d) public static boolean loadDriver(java.lang.String driverClassName): This party loads and registers the JDBC driver, and returns true if successful. Using this method, you don't need to catch this exception ClassNotFoundException.

2. QueryRunner class: This class simplifies SQL queries, and can be used in combination with resultSetHandler to complete most of the database operations, which can greatly reduce the amount of coding.

a)  Default constructor: generally if it is the default. It is necessary to obtain a Connection object externally, which is generally used for transaction control.

b)  The constructor of the DataSource needs to be injected : there is no need to create a Connection object externally .

c) public Object query(Connection conn, String sql, Object[] params, ResultSetHandler rsh) throws SQLException : perform a query operation, in this query, the value of each element in the object array is used as the replacement parameter of the query statement . This method handles the creation and closing of PreparedStatement and ResultSet by itself.

d) public Object query(String sql, Object[] params, ResultSetHandler rsh) throws SQLException : almost the same as the first method; the only difference is that it does not provide the database connection to the method, and it is provided from the constructor Data source (DataSource) or use the setDataSource method to regain the Connection

e) public Object query(Connection conn, String sql, ResultSetHandler rsh) throws SQLException : Execute a query operation that does not require substitution parameters.

f) public int update(Connection conn, String sql, Object[] params) throws SQLException: used to perform an update (insert, update or delete) operation.

g) public int update(Connection conn, String sql) throws SQLException : used to perform an update operation that does not require replacement parameters.

3. ResultSetHandler interface: handle ResultSet and hand over data to this type of processing. Its method Object handle (java.sql.ResultSet.rs) is used to process the data. The implementation classes of common subclass functions are as follows

a) ArrayHandler: Convert the first row of data in the result set into an array of objects

b) ArrayListHandler : Convert each row of data in the result set into an array and store it in List .

c) BeanHandler : encapsulate the first row of data in the result set into a corresponding JavaBean instance

d) BeanListHandler : Encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List .

e) ColumnListHandler : Store the data of a column in the result set in the List .

f) KeyedHandler(name) : Encapsulate each row of data in the result set into a Map< column name , column value > , and then store these maps in a map whose key is the specified key .

g) MapHandler : encapsulates the first row of data in the result set into a Map , the key is the column name, and the value is the corresponding value

h) MapListHandler : encapsulate each row of data in the result set into a Map , and then store it in List

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325409408&siteId=291194637