Deeply sqlite the problem of multithread warehousing _java

Source: Internet
Author: User
Tags rollback sqlite
Today, the manager gave me a SQL file of more than 30 m to test the data positioning problem. By convention, I create a table using the Navicat for SQLite, and then import the SQL file. I then went to do other things, about one hours, I think the data should be imported almost. I opened it, and I was sweating and died there. I turn off the software and import it again, or that virtue. Also learned that the manager once himself has guided, no success. It seems that the method of importing tools is not workable.

But, think about Grovan data, is Grovan Insert SQL statement, is that difficult? So, I would like to write a program to import it. Although the middle also encountered some minor episodes, but still successfully put the data into the.
the code for the program is as follows:
Copy Code code as follows:

Package com.geoway.pad.common.tool;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.TimeUnit;
/**
* @author Likehua
* @note SQLite library and bulk storage
* */
public class batchtool{
Ddl
private static String ddl= "CREATE TABLE IF not EXISTS pbeijing_point (OBJECTID integer,name text,address text,phone TEX T,fax text,type text,citycode text,url text,email text,name2-text,x INTEGER) ";
Connection Jcon=null;
Get connection
Public synchronized Connection getconnection () {
if (jcon==null) {
json=
Statement State=null;
try {
Class.forName ("Org.sqlite.JDBC");
Jcon=drivermanager.getconnection ("jdbc:sqlite:c:\\newd.db");
State=jcon.createstatement ();
State.executeupdate (DDL);
catch (SQLException e) {
E.printstacktrace ();
catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}
return Jcon;
}
Create 500 threads
Executorservice Service=executors.newfixedthreadpool (500);
Read SQL files Every 500 INSERT statements are bulk manipulated by one thread
public void Readbatchsql (InputStream is) throws ioexception{
BufferedReader bufferreader=new BufferedReader (New InputStreamReader (IS, "UTF-8"));
String Line;
String one= "";
int tag=0;
String batchsql= "";
while ((Line=bufferreader.readline ())!=null) {
One+=line;
if (One.indexof (";")! =-1) {
Batchsql+=one;
One= "";//reset
tag++;
};
Meet the conditions to open up a thread
if (tag!=0&&tag/500!=0) {
Service.execute (New Sqlitebatchhandler (Batchsql));
Batchsql= "";//reset
Tag=0;//reset
}
}
Finally execute the remaining SQL
if (Batchsql.length () >0) {
System.out.println ("Finalsql:" +batchsql);
Runnable r=new Sqlitebatchhandler (batchsql);
Service.execute (R);
};
try {
Close the thread pool
This.service.shutdown ();
This.service.awaitTermination (1, timeunit.hours);<br> getconnection (). Close ();<br>} CATC H (interruptedexception e) {
E.printstacktrace ();
catch (SQLException e) {
E.printstacktrace ();
}

};
/**
* @note Split SQL
* */
private static string[] Splitsql (String batchsql) {
if (batchsql!=null) {
return Batchsql.split (";");
};
return null;
}
/**
* @note Perform bulk update operations
* Because of the connection.comit operation, if there is a statement without close, the error is added to synchronize this method.
* */
Private synchronized void Exucteupdate (String batch) {
Statement Ste=null;
Connection Con=null;
try{
Con=getconnection ();
Con.setautocommit (FALSE);
Ste=con.createstatement ();
String[] Sqls=this.splitsql (batch);
for (String Sql:sqls) {
if (sql!=null) {
Ste.addbatch (SQL);
};
};
Ste.executebatch ();<br> ste.close ();
Con.commit ()/Submit
}catch (Exception e) {
E.printstacktrace ();
System.out.println ("Execution failed:" +batch);
try {
Con.rollback ();/rollback
catch (SQLException E1) {
E1.printstacktrace ();
}
}finally{
if (ste!=null) {
try {
Ste.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}
}
}
/**
* @author Likehua
* @note Inbound Thread
* */
Private class Sqlitebatchhandler implements runnable{
private String batch;
Public Sqlitebatchhandler (String sql) {
This.batch=sql;
};
@SuppressWarnings ("Static-access")
@Override
public void Run () {
try {
Thread.CurrentThread (). Sleep (50);
catch (Interruptedexception e) {
E.printstacktrace ();
}
if (This.batch.length () >0) {
Exucteupdate (Batch);

};

}
}
/**
* @author Likehua
* @note Main function entry
* */
public static void Main (string[] args) throws FileNotFoundException, ioexception{
Batchtool s=new Batchtool ();
S.readbatchsql (New FileInputStream ("C:\\poi.sql"));
}
}

Related Article

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.