Mysql Three batch increase performance analysis _mysql

Source: Internet
Author: User
The following code to write out, I hope you criticize correct.
First domain object. The annotation used here is in the form of a relatively new version.
User.java
Copy Code code as follows:

Package com.bao.sample.s3h4.domain;
Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import javax.persistence.Table;
Import Com.bao.sample.base.domain.BaseDomain;
@Entity
@Table (name = "T_user")
public class User extends Basedomain {
Private static final long serialversionuid = 1L;
private int id;
Private String username;
private String password;
/**
* @Description annotations are best marked on the Get method. Note: In a consistent notation, annotations are marked by the ID, and if the tag is on the Get method, the annotation on the property is ignored.
* @return
*/
@Id
@GeneratedValue (strategy = generationtype.identity)
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
@Column (nullable = False)
Public String GetUserName () {
return username;
}
public void Setusername (String username) {
This.username = Username;
}
@Column (nullable = False)
Public String GetPassword () {
return password;
}
public void SetPassword (String password) {
This.password = password;
}
Public User () {
Super ();
}
public User (int ID, string username, string password) {
Super ();
This.id = ID;
This.username = Username;
This.password = password;
}
}

Next is the DAO interface, which inherits a Basedao interface.
Copy Code code as follows:

Package Com.bao.sample.s3h4.dao;
Import java.util.List;
Import Com.bao.sample.base.dao.BaseDao;
Import Com.bao.sample.s3h4.domain.User;
Public interface Userbatchdao extends basedao<user> {
/**
* @Description Bulk Increase operation
* @return-1: Operation failed; 0: normal execution; >0: number of successful executions
*/
public int Batchaddusingjdbc (list<user> users);
public int batchaddusinghibernate (list<user> users);
public int batchaddusingjdbctemplate (list<user> users);
}

Implementation of Userbatchdao:
Copy Code code as follows:

Userbatchdaoimpl
Package Com.bao.sample.s3h4.dao;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import java.sql.SQLException;
Import java.util.List;
Import Javax.annotation.Resource;
Import org.hibernate.Session;
Import Org.springframework.jdbc.core.BatchPreparedStatementSetter;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.orm.hibernate4.SessionFactoryUtils;
Import Org.springframework.stereotype.Repository;
Import org.springframework.transaction.annotation.Transactional;
Import Com.bao.sample.base.dao.BaseDaoImpl;
Import Com.bao.sample.s3h4.domain.User;
/**
*
* @Description three ways to increase the number of batches, the implementation efficiency in turn is JDBC, JdbcTemplate, hibernate.<br/>jdbc and jdbctemplate execution efficiency, However, JdbcTemplate can use transaction annotation control, so it is preferred.
* @author Bob hehe198504@126.com
* @date 2012-8-13
*/
@Repository ("Userbatchdao")
public class Userbatchdaoimpl extends basedaoimpl<user> implements Userbatchdao {
@Resource
protected JdbcTemplate JdbcTemplate;
/**
* Execute 10W record, roughly time consuming 15188ms
*/
@Override
public int Batchaddusingjdbc (list<user> users) {
int result = 0;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "INSERT into T_user (Username,password) VALUES (?,?)";
try {
conn = Sessionfactoryutils.getdatasource (sessionfactory). getconnection ();
Conn.setautocommit (FALSE);
pstmt = conn.preparestatement (sql);
for (int i = 0; i < users.size (); i++) {
int j = 1;
Pstmt.setstring (j + +, Users.get (i). GetUserName ());
Pstmt.setstring (j + +, Users.get (i). GetPassword ());
Pstmt.addbatch ();
}
Pstmt.executebatch ();
Conn.commit ();
Conn.setautocommit (TRUE);
catch (SQLException e) {
IF (conn!= null) {
try {
Conn.rollback ();
catch (SQLException E1) {
E1.printstacktrace ();
}
}
finally {
if (pstmt!= null) {
try {
Pstmt.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
IF (conn!= null) {
try {
Conn.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
}
return result;
}
/**
* Execute 10W record, roughly time consuming 131203ms, roughly 10 times times as much as JDBC or jdbctemplate.
*/
@Override
@Transactional (norollbackfor = runtimeexception.class)
@Transactional
public int batchaddusinghibernate (list<user> users) {
Session session = This.getsession ();
for (int i = 0; i < users.size (); i++) {
Session.save (Users.get (i));
After adding 20, force the storage
Clear () Empty cache
The isolation level of the Postgres database is committed read (read Committed),
So after the flush, the data can not be seen, only after a commit to see the data,
If it fails, rollback, the preceding flush data will not be in storage
if (i% 20 = 0) {
Session.flush ();
Session.clear ();
}
}
return 0;
}
/**
* Execute 10W record, roughly time consuming 15671ms
*/
@Transactional (norollbackfor = runtimeexception.class)
@Transactional
public int batchaddusingjdbctemplate (list<user> users) {
String sql = "INSERT into T_user (Username,password) VALUES (?,?)";
Final list<user> tempusers = users;
Final int count = Users.size ();
BatchPreparedStatementSetter pss = new BatchPreparedStatementSetter () {
Sets the parameters for the prepared statement. The number of times this method will be invoked throughout the process
public void Setvalues (PreparedStatement pstmt, int i) throws SQLException {
int j = 1;
Pstmt.setstring (j + +, Tempusers.get (i). GetUserName ());
Pstmt.setstring (j + +, Tempusers.get (i). GetPassword ());
}
Returns the number of result set bars updated
public int getbatchsize () {
return count;
}
};
Jdbctemplate.batchupdate (SQL, PSS);
return 0;
}
Public JdbcTemplate getjdbctemplate () {
return jdbctemplate;
}
}

Outside the frame is not attached, there is a need to leave a message, I provide package download.
Author: Listen to the rain porch

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.