Javaweb Series 13 (JDBC Transaction and connection pooling)

Source: Internet
Author: User
Tags connection pooling one table stub

1. Transaction: Indicates that a set of operations either succeeds, has a failure, and all operations fail. To use a transaction in MySQL, start the transaction first, the statement start transaction, commit the transaction: Commit ROLLBACK TRANSACTION: rollback. Transfer: 2.JDBC transaction operation (1) method of operation Transaction Setautocommit (Boolean autocommit) sets the way the transaction is submitted, default autocommit, set to Fals COMMIT TRANSACTION commit () ROLLBACK TRANSACTION Roll Back () use JDBC to complete the transfer Kim and Xiao Li 1000 (2) Set the rollback point of the transaction to commit, commit (SQ), and return to the place where the rollback point is set 3. Features of the transaction: four features:1. atomicity: Operating within a transaction, either success or failure2. Consistency: In the process of transactional operations, the data should be consistent, such as no more money, no less3. Isolation: Multiple transactions simultaneously operate on one table, and multiple transactions do not affect each other4. Persistence: Commit the transaction to the database, is really effective4. Isolation of transactions does not consider the isolation of transactions, resulting in a number of problems, five questionsthree read-right questions: The first problem: Dirty reads, a transaction gets the uncommitted transactions to another transactionThe second problem: non-repeatable read, a transaction inside the query to the committed transaction, the other transaction is update, resulting in two transactions to query the data is not the same. The third problem: virtual read: One transaction inside the query to another transaction inside the insert OperationWorkaround: Resolve these read issues through the isolation level Serializable:Serial. Avoid dirty reads, non-repeatable reads, and virtual read occurrences Repeatable READ:Repeat read. Avoid dirty reads and non-repeatable reads. But there is still a possibility that the virtual reading will happen, the default is this, two transactions need to commit, in order to query the Read committed:Read Committed. Avoid dirty reading. But the non-repeatable read and the virtual reading is possible, when submitting, can inquire the money, avoids the loss.causes two transactions to query the data is not the same Read Uncommitted: READ UNCOMMITTED. Dirty read, non-repeatable read, virtual read all may occur without committing to the content can be queried, if the rollback, will not find money.  SELECT @ @tx_isolation; Querying the current transaction isolation level set Session transaction Isolation Levels setting transaction isolation level (1) Show dirty read First step: Open two cmd window Step two: Set the isolation Level Three steps: Start a transaction Fourth step: View data on the left Read committed:In the left window through the food to view the data, these two see different results, multiple transactions between the impression, non-repeatable read 5.mysql database backup and restore the first: Using a visualizer for backup the second type: using the SQL statement mysqldump-u root- P The data name to be backed up > the disk to be put back in the database statement backed up by this statement, there is no statement to create the database Note 1: Cannot connect to the database Note 2:There are no statements to create a databaseNote 3: The backup file is to be suffixed with. SQLNote 4: The backup path cannot have Chinesemysqldump-u root-p day16 >d:\day17.sqlRestore the database mysql-u root-p the file path of the database < Backup to restore NOTE 1: The executed statement cannot be written after the semicolon Note 2: Do not log on 6. Get a database connection before the overview of connection pooling Drivermanager.getconnect    Ion (); Each user connection needs to be executed once, using the connection pool to solve the pre-creation of some connections, put into memory of a space, each time access, from the memory space to obtain a link, after the use of not closed, put back into the memory space for other users to use.    Avoid frequent creation and shutdown in memory space understood as a container in which the database is placed inside the connection. Custom connection pooling (learn)create a class to implement an interface datasource Allocates a spatial list collection in memory, places the created connections into the collection and puts the created connection into the collection. Add () Remove the link, remove (0)
Open-Source connection pooling dbcp Connection Poolc3p0 Connection pool (this connection pool is used in the general enterprise)Tomcat built-in connection pool
How to enhance a method of a class first inherit public class TestMethod {public static void main (string[] args) {Man M=new Superman (); M.run1 ();   }} class man{public void Run1 () {System.out.println ("nam Run");    }} class Superman extends mans {public void run1 () {//super.run1 ();    System.out.println ("Supernam run"); }} The second decorator pattern first creates an interface that defines a method to write two classes in the interface, and the two classes implement this interface in the class of the enhanced method, and get the reference to the class where the enhanced method is located. public class Te    stMethod2 {public static void main (string[] args) {Diao d=new Diao (New Ying ());    D.fly (); }   } interface bird{public void fly ();     } class Ying implements bird{public void Fly () {//TODO auto-generated Method stub System.out.println (1000); }} class Diao implements bird{private Bird Bird; Public Diao (Bird Bird) {this.bird=bird;    } public void Fly () {//TODO auto-generated Method Stub bird.fly ();    SYSTEM.OUT.PRINTLN (0); }   The third dynamic Agent 9. Open Source Connection PoolDBCP Connection pool: Apache software Organization, open source connection pool, first import jar package  Basicdatasource: Manual configuration file Modebasicdatasourcefactory: automatic configuration file, the name of the profile is fixed and cannot be changed C3P0 connection pool want to use C3P0 connection pool, the jar package combopooleddatasource Complete connection pooling connections The first is manually configured to create Class Combo.setdriverclass ("Com.mysql.jdbc.Driver");        Combo.setjdbcurl ("Jdbc:mysql:///day18");        Combo.setuser ("root");        Combo.setpassword ("root"); The second configuration creates a configuration file, not the properties format, which is an XML configuration fileNote the name of the 1:xml is fixed c3p0-config.xml., NOTE 2: The configuration file should be placed under SRC, not under the other, in New Combopooleddatasource ("") do not write or write wrong, find the default read defalut-config inside the information, The Jndi technology used in Tomcat's built-in connection pool requires a class in tomcat that wants to invoke the class, give the Class a name, and access the class by that name. Step One: Create a configuration file, name fixed con Text.xml This file needs to be created in can have three places the first can be created under the Conf directory: all virtual hosts in Tomcat can use the second can create files \conf\catalina\localho ST: only in the current virtual host using the third can be created in the Meta-inf: can only be used in the current project (generally used in this way)Step two: Create a servlet to implement a connection pool because of the Tomcat built-in connection pool, you must run it inside Tomcat to use this connection pool









Javaweb Series 13 (JDBC Transaction and connection pooling)

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.