Multi-process write SQLite Solution

Source: Internet
Author: User

This blog post is based on the previous blog article. Questions about the previous blog:

It is recommended that you read the problem first Background: http://blog.csdn.net/xinwang24/article/details/7311944

There are two solutions I have tried. The first one is to use a file lock for processing, but the effect is not satisfactory.

This article describes how to use socket to write data into the SQLite database to process multiple processes (independent processes rather than threads.

The Code is as follows: (the code here is modified based on the previous blog)

Modify dB. Java

Package COM. socket. SQLite; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import java.net. socketexception; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. util. arraylist; import Java. util. list; import Java. util. concurrent. executorservice; public class dB {private Static connection = NULL; Private Static preparedstatement = NULL; private resultset = NULL; private string name = NULL; Private Static final int Port = 8000; // port number Private Static list <socket> List = new arraylist <socket> (); // Save the connection object private executorservice exec; Private Static serversocket Server = NULL; public dB (string name) {This. name = Name;} Private Static Ser Versocket getserversocket (INT port) {serversocket Server = NULL; try {Server = new serversocket (port); system. out. println ("port bound successfully"); return server;} catch (ioexception e) {system. out. println ("failed to bind port"); return NULL ;}} private socket getsocketok (serversocket) {try {serversocket. setsotimeout (10000);} catch (socketexception E1) {e1.printstacktrace ();} Socket socket = NULL; try {socket = serverso. Accept ();} catch (ioexception e) {e. printstacktrace (); return NULL;} return socket;} public synchronized Boolean putone (entity one) {serversocket Server = getserversocket (port); If (server! = NULL) {system. out. println ("successfully preemptible port"); If (isexist (one. getname () {system. out. println ("the task already exists"); Return false;} else {string SQL = "insert into '" + constants. hiep_tb + "'values (?,?) "; Try {preparedstatement = connection. preparestatement (SQL); preparedstatement. setstring (1, one. getname (); preparedstatement. setstring (2, one.getage(registry.preparedstatement.exe cuteupdate (); system. out. println (name + "successful data insertion"); Return true;} catch (sqlexception e) {e. printstacktrace (); Return false;} finally {try {server. close (); system. out. println ("Release port"); try {thread. sleep (50);} catch (interruptedexception E) {e. printstacktrace () ;}} catch (ioexception e) {e. printstacktrace () ;}}} else {// No sockettry {thread is obtained. sleep (3000);} catch (interruptedexception e) {e. printstacktrace (); Return false;} putone (one); Return false;} public arraylist <entity> getall () {arraylist <entity> List = new arraylist <entity> (); entity one = new entity (); string SQL = "select * From hieptb"; try {preparedstatement = connection. PR Eparestatement (SQL); resultset = preparedstatement.exe cutequery (); While (resultset. next () {one. setname (resultset. getstring ("name"); one. setage (resultset. getstring ("Age"); list. add (one);} return list;} catch (sqlexception e) {e. printstacktrace (); return NULL ;}} public static Boolean Init () {try {class. forname (constants. jdbc_url); connection = drivermanager. getconnection (constants. connection_url + Consta NT. db_name);} catch (classnotfoundexception e) {system. out. println ("the database driver class cannot be found"); Return false;} catch (sqlexception e) {system. out. println ("SQL exception"); Return false;} string SQL = "CREATE TABLE if not exists" + constants. hiep_tb + "'(Name text, age text);"; try {preparedstatement = cipher cute (); Return true;} catch (sqlexception E1) {e1.printstacktrace (); Re Turn false ;}} private Boolean isexist (string name) {string SQL = "select * from '" + constants. hiep_tb + "'where name =? "; Try {preparedstatement = connection. preparestatement (SQL); preparedstatement. setstring (1, name); resultset = preparedstatement.exe cutequery (); If (resultset. next () {return true ;}} catch (sqlexception e) {e. printstacktrace (); Return false;} return false ;}}

Modifies the Thread class putter. Java for database writing.

Package COM. socket. SQLite; import Java. io. ioexception; import java.net. socket; import java.net. unknownhostexception;/*** want to store data in the database **/public class putter implements runnable {private dB DB = NULL; private string name = NULL; private Socket socket = NULL; private final string host = "localhost"; private final int Port = 8000; Public Putter (string name) {This. name = Name; this. DB = new dB (name) ;}@ overridepublic void run () {for (INT I = 0; I <10000; I ++) {/* try {socket = new socket (host, Port);} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} * // system. out. println ("client connection successful"); dB. putone (New Entity (this. name + "wangxin [" + I + "]");/* try {socket. close ();} catch (ioexception e) {e. printstacktrace ();}*/}}}

Test scenario: Process 1:

Package COM. socket. SQLite; public class testmain2 {public static void main (string ARGs []) {If (dB. init () {system. out. println ("preliminary trial completed");} else {system. out. println ("initialization failed"); return;} new thread (new putter ("testmain2putter1 ")). start (); New thread (new putter ("testmain2putter2 ")). start (); New thread (new putter ("testmain2putter3 ")). start (); New thread (new putter ("testmain2putter4 ")). start (); New thread (new putter ("testmain2putter5 ")). start (); New thread (new putter ("testmain2putter6 ")). start (); New thread (new putter ("testmain2putter7 ")). start (); New thread (new putter ("testmain2putter8 ")). start (); New thread (new reader ()). start ();}}

Test process 2 ":

Package COM. socket. SQLite; public class testmain1 {public static void main (string ARGs []) {If (dB. init () {system. out. println ("preliminary trial completed");} else {system. out. println ("initialization failed"); return;} new thread (new putter ("testmain1putter1 ")). start (); New thread (new putter ("testmain1putter2 ")). start (); New thread (new putter ("testmain1putter3 ")). start (); New thread (new putter ("testmain1putter4 ")). start (); New thread (new putter ("testmain1putter5 ")). start (); New thread (new putter ("testmain1putter6 ")). start (); New thread (new putter ("testmain1putter7 ")). start (); New thread (new putter ("testmain1putter8 ")). start (); New thread (new reader ()). start ();}}

Test passed. Okay, detailed code: http://download.csdn.net/detail/xinwang24/4126177

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.