What is the isolation of a transaction
Isolation is important for data consistency. Online has a lot of introduction, recommended reading: MySQL official website 14.5.2.1 Transaction isolation levels Hibernate Tutorial 20.7.2 Transaction the isolation Levels
In general, if you use read_uncommitted, which is appropriate for a read-only table, you do not have to wait to read the same item, but for a read-write table, if we read a data and then modify and write it, we need to use read_committed, Repeatable_ Read or serializable, they put the reading operation into the transaction, and if you use read_uncommitted, there is a risk of data clutter (dirty data). Among them serializable careful use, it is easy to cause deadlock. Specifically, depending on the requirements, the following is the introduction of Javadoc:
connection.transaction_read_uncommittedA constant indicating that dirty reads, non-repeatable reads and phantom reads can occur. This level allows a row changed by one transaction to is read by another transaction before any changes in that row have B Een committed (a "dirty read"). If any of the changes are rolled, the second transaction'll have retrieved an invalid row.connection.transaction_read_committedA constant indicating that dirty reads are prevented; Non-repeatable reads and phantom reads can occur. This level is prohibits a transaction from reading a row with uncommitted changes in it.Connection.transaction_repeatable_readA constant indicating that dirty reads and non-repeatable reads are; Phantom reads can occur. This level prohibits a transaction to reading a row with uncommitted changes in it, and it also prohibits the situation Where one transaction reads a row, a second transaction alters the row, and the The "the", transaction the row, rereads Different values the second time (a "non-repeatable read").connection.transaction_serializableA constant indicating that dirty reads, non-repeatable reads and Phantom reads are. This level includes the prohibitions in Transaction_repeatable_read and further prohibits the situation where one Transact Ion reads all rows then satisfy a where condition, a second transaction inserts a row that satisfies where condition, And the the transaction rereads for the same condition, retrieving the additional "phantom" row in the second read.
isolaction settings for data sources
Tomcat Data SourceThe isolation is set for the data source. If we take Tomcat's data source, we just need to make a global configuration.
<resource name= "Jdbc/learntest" type= "Javax.sql.DataSource" maxactive= "
5
" maxidle= "maxwait=" Username= "Test" password= "test123456"
driverclassname= "Com.mysql.jdbc.Driver"
defaulttransactionisolation= "read_committed"
url= "Jdbc:mysql://191.8.1.107:3306/test"/>
c3p0 Data Source
In general, we recommend using Tomcat to set up a data source, because in the war package, you do not need to consider encapsulating the MySQL-related packages, nor do you need to be careful with the shutdown of the data source in the uninstall. However, for some reasons, such as dynamically acquiring configuration information for a database after the war is started, it is difficult to use the configuration of a static Tomcat data source. The use of the provision of database connection management pool for C3P0, we have also introduced in the code before, again borrow isolation opportunity to elaborate again. How to create a C3P0 data source and safely shut it down for easy use, we use C3p0utils to create the data source and provide a destory () way to safely close the data source when the war shuts down.
public class C3p0utils {private static final Logger Logger = Logmanager.getlogger ();
private static final map<string,combopooleddatasource> datasourcedb = new hashmap<> (); public static Combopooleddatasource open (string name,string jdbcurl,string User, string pw, int minpoolsize,int Maxpo Olsize,int poolincrement) throws exception{if (Datasourcedb.containskey (name)) throw new Exception ("The data source already exists, please do not create it repeatedly."
");
Combopooleddatasource DataSource = new Combopooleddatasource ();
Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setjdbcurl (Jdbcurl + "? usessl=true&useunicode=true&characterencoding=utf-8&autoreconnect=
True ");
Datasource.setuser (user);
Datasource.setpassword (PW);
Datasource.setminpoolsize (minpoolsize);
Datasource.setacquireincrement (poolincrement);
Datasource.setmaxpoolsize (maxpoolsize);
Datasource.setinitialpoolsize (minpoolsize);
Datasource.setmaxidletime (30); Datasource.setidleconnectiontestperiod (30); The following sentence is related to setting isolation, and we will introduce Datasource.setconnectioncustomizerclassname ("Com.wei.utils.MyConnectionCustomizer") later.
); /* Check that the data source is valid.
C3P0 does not connect when the datasource is created, but on the first connection request. * On the one hand, will increase the first user request processing time, affecting the first user experience; * On the other hand, if the password is wrong or the address is inaccessible, we need to know if the deployment is right the first time the project is deployed, rather than waiting until the business comes.
* * try{Connection conn = Datasource.getconnection ();
Conn.close (); }catch (Exception e) {Datasources.destroy (DataSource);
or Datasource.close () throw new Exception ("DataSource Open Error:" + e.tostring ());
} datasourcedb.put (name, DataSource); Logger.info ([INIT] opened DataSource ' {} ' ...
[OK] ", name);
return dataSource; /** * All resources should be released at the end of the project and this method should be called.
Suggest reading C3P0 official website.
*/public static void Destroy () {Logger.info ("[Close] close DataSource ...");
Iterator<map.entry<string,combopooleddatasource>> iter = Datasourcedb.entryset (). Iterator ();
while (Iter.hasnext ()) {map.entry<string,combopooleddatasource> Entry = Iter.next (); Combopooleddatasource DAtasource = (Combopooleddatasource) entry.getvalue ();
Datasource.close ();
} datasourcedb.clear ();
Com.mysql.jdbc.AbandonedConnectionCleanupThread.checkedShutdown ();
enumeration<java.sql.driver> drivers = drivermanager.getdrivers ();
while (Drivers.hasmoreelements ()) {try {Driver Driver = drivers.nextelement ();
Drivermanager.deregisterdriver (driver);
catch (SQLException e) {logger.error ("C3m0 destroy error: {}", e.tostring ()); }
}
}
}
In the context configuration:
@Bean public
DataSource Springjpadatasource () throws exception{return
c3p0utils.open ("Learntest", "JDBC: Mysql://192.168.1.2:3306/test "," Test "," test123456 ", 2, 2);
set the isolation level of the C3P0If you use C3P0, it's a little more complicated. The default for C3P0 may be none, or read_uncommitted, which I'm not sure. This can be accomplished by customizing the Connectioncustomizer interface:
The public class Myconnectioncustomizer implements connectioncustomizer{@Override the public
void Onacquire ( Connection C, String Pdsidt) {
//override the default transaction isolation of newly acquired connections
C.sett Ransactionisolation (connection.transaction_serializable);
}
@Override public
void OnDestroy (Connection C, String Pdsidt) {
}
@Override public
void Oncheckout (Conn Ection c, String Pdsidt) {
}
@Override public
void Oncheckin (Connection C, string Pdsidt) {
}
}
When you create a C3P0 data source, you need to set up a custom-modified connection interface.
Datasource.setconnectioncustomizerclassname ("Com.wei.utils.MyConnectionCustomizer");
RELATED links: My professional Java for WEB applications related articles