mysqlの3種類の大量増加の性能分析


コードを書いてください。ご指摘をお願いします。まずdomannオブジェクトです。ここで使っている注釈は比較的新しいバージョンです。User.java
 
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 get . : , Id , get , property .
* @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;
}
}
はDaoインターフェースで、BaseDaoインターフェースを継承します。
 
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
* @return -1: ;0: ;>0:
*/
public int batchAddUsingJdbc(List<User> users);
public int batchAddUsingHibernate(List<User> users);
public int batchAddUsingJdbcTemplate(List<User> users);
}
UserBatch Daoの実現:
 
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 , jdbc、jdbcTemplate、hibernate.<br />jdbc jdbcTemplate , jdbcTemplate , .
* @author Bob [email protected]
* @date 2012-8-13
*/
@Repository("userBatchDao")
public class UserBatchDaoImpl extends BaseDaoImpl<User> implements UserBatchDao {
@Resource
protected JdbcTemplate jdbcTemplate;
/**
* 10W , 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;
}
/**
* 10W , 131203ms, jdbc jdbcTemplate 10 .
*/
@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));
// 20 ,
// clear()
// postgres (Read committed),
// flush , , commit ,
// ,rollback, flush
if (i % 20 == 0) {
session.flush();
session.clear();
}
}
return 0;
}
/**
* 10W , 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() {
// prepared statement 。
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());
}
//
public int getBatchSize() {
return count;
}
};
jdbcTemplate.batchUpdate(sql, pss);
return 0;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
}
周辺のフレームは添付されていません。メッセージが必要です。パッケージダウンを提供します。作者:雨軒軒を聞きます。