发表于: 2017-05-22 23:57:16
6 1066
今天所做:
对照着施星师兄推荐的师兄日志把线程池和连接池那部分代码敲了一遍,实现了这个。
师兄是自己写了连接池,创建了1000个线程。每个线程插入一条数据。
package com.dj.ConnectionPool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ConnectionPool {
//集合
List<Connection> ls = new ArrayList<Connection>();
int size;
//构造方法 初始化
public ConnectionPool(int size){
this.size = size;
init();
}
private void init() {
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DBURL = "jdbc:mysql://139.199.74.115:3306/students?useSSL=false";
final String DBUSER = "root";
final String PASSWORD = "123456";
try {
//注册驱动
Class.forName(JDBC_DRIVER);
//获取连接 连接池中获取的连接数
for(int i = 0;i<size;i++){
Connection c = DriverManager.getConnection(DBURL,DBUSER,PASSWORD);
//集合中添加一个Connection对象
ls.add(c);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public synchronized Connection getConnection(){
while(ls.isEmpty()){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Connection c = ls.remove(0);
return c;
}
//返还连接
public synchronized void returnConnection(Connection c){
ls.add(c);
this.notify();
}
}
---------
package com.dj.ConnectionPool;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.dj.service.StudentService;
public class WorkingThread extends Thread{
private static Logger logger = LogManager.getLogger(StudentService.class);
private ConnectionPool cp;
//构造函数 创建线程时
public WorkingThread(String name,ConnectionPool cp){
super(name);
this.cp = cp;
}
public void run(){
Connection c = cp.getConnection();
System.out.println(this.getName()+":\t获取连接,并开始了工作");
try(Statement st = c.createStatement()){
Thread.sleep(100);
String sql = " INSERT INTO student1 VALUES "
+ "('1','小白','java','20170516','hust','111','11111','111','20170522','20170522');";
st.execute(sql);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
cp.returnConnection(c);
}
}
------------
package com.dj.ConnectionPool;
public class TestConnectionPool {
public static void main(String[] args) {
ConnectionPool cp = new ConnectionPool(3);
for (int i = 0; i < 1000; i++) {
new WorkingThread("Working thread"+i,cp).start();
}
}
}
-------------
然后是测试插入数据,中间出了一些千奇百怪的错,改到不报错了,得到下面的结果,但实际上navicat里面没有新数据。
package com.dj.ConnectionPool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import com.mysql.jdbc.PreparedStatement;
public class MyThread extends Thread {
public void run() {
String url = "jdbc:mysql://139.199.74.115:3306/students?useSSL=false";
String name = "com.mysql.jdbc.Driver";
String user = "root";
String password = "123456";
Connection conn = null;
try {
Class.forName(name);
conn = DriverManager.getConnection(url, user, password);
// 关闭自动提交
conn.setAutoCommit(false);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Long begin = new Date().getTime();
String prefix = "INSERT INTO student1(id,name,type,time,school,online_id,wish,rec_senior,create_at,update_at) VALUES";
try {
StringBuffer suffix = new StringBuffer();
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("");// 准备执行语句
// 外层循环,总提交事务次数
for (int i = 1; i <= 10; i++) {
suffix = new StringBuffer();
for (int j = 1; j <= 10000; j++) {
suffix.append("('"+ "1"+ "','"+ "小黑'"+ ",'java'"
+ ",'20170516'"
+ ",'hust'"
+ ",'111'"
+ ",'11111'"
+ ",'111'"
+ ",'20170522'"
+ ",'20170522')"
+ ",");
}
String sql = prefix + suffix.substring(0, suffix.length() - 1);
pst.addBatch(sql);
pst.executeBatch();
suffix = new StringBuffer();
}
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Long end = new Date().getTime();
System.out.println("100万条数据插入花费时间 : " + (end - begin) / 1000 + " s" + " 插入完成");
}
}
------------
package com.dj.ConnectionPool;
public class Test {
public static void main(String[] args) {
for (int i = 1; i <10; i++) {
new MyThread().start();
}
}
}
把线程改为1时,不报错,但数据没插入新数据。
package com.dj.ConnectionPool;
public class Test {
public static void main(String[] args) {
for (int i = 1; i <10; i++) {
new MyThread().start();
}
}
}
所遇问题:
例子中用的jdbc连接数据库插入数据。我想用spring里dao在数据库插入数据,用junit来测试。
想用
@Autowired
private SpringStudentDao springstudentDao ;获取dao插入数据。但是都报 空指针异常。
只好换成师兄这种,但是这样的话插入的都是重复的数据,我因此还取消了id主键和自增。
收获:
对多线程略有了解。
试了错。
今天在jdbc方法id不能使用自增,用spring又报空指针不能获取到dao中循环循环。
明天要做:
跳出去。
好好学习。
评论