The project at hand uses the Spring MVC framework, which, given the efficiency of execution, requires access to the database with native SQL under multithreading. Leave a note for the memo.
The basic configuration is not said, there are many tutorials on the internet.
Main name class:
@Component ("Manage")
@Transactional Public
class manage implements Imanage {
private String ID;
Private map<string,string> Sqlmap;
Private multithread multithread;
* * multithread Inherits callable interface, executes child process with return value
/@Override public
multithread Getmultithread () {
return multithread;
}
@Resource (name= "multithread") public
void Setmultithread (multithread multithread) {
This.multithread = multithread;
}
Public map<string, String> Getsqlmap () {return
sqlmap;
}
* * @param sqlmap dynamically generated SQL statement *
/public void Setsqlmap (map<string, string> sqlmap) {
This.sqlmap = Sqlmap;
}
public void SetId (String id) {
this.id = ID;
}
* * @param resultmap Save the result set after SQL Run
/@Override public
Map Getdatabyid (String id) {
This.multiThread.setSqlMap (NEWSQLMAP);
Map Resultmap = This.multiThread.createThread ();
Return Resultmap
}
}
Build Multithreading:
@Component ("multithread") public class Multithread {private Map sqlmap;
Private Sessionfactory sessionfacotry;
Private Countdownlatch Endsingle;
Public Sessionfactory Getsessionfacotry () {return sessionfacotry; * * * Spring container injection sessionfactory/@Resource (name= "refseqsessionfactory") public void Setsessionfacotry (sessionfact
Ory sessionfacotry) {this.sessionfacotry = Sessionfacotry;
Public Map Getsqlmap () {return sqlmap;
The public void Setsqlmap (Map sqlmap) {this.sqlmap = Sqlmap;
This.endsingle = Endsingle;
Public multithread (Map sqlmap) {super ();
This.sqlmap = Sqlmap;
Private multithread () {super (); * * * @param endsingle Thread counter * @param pool thread pool/public Map CreateThread () {Countdownlatch endsingle = new Cou
Ntdownlatch (This.sqlMap.size ());
Map Resultmap = new HashMap ();
Executorservice pool = Executors.newcachedthreadpool ();
Iterator iterator = This.sqlMap.keySet (). iterator (); Future<?> Future = null;
while (Iterator.hasnext ()) {//TableName is the key of resultmap String tablename = (string) iterator.next ();
String sql = (string) this.sqlMap.get (tablename); if (tablename!= null && SQL!= NULL) {//new child thread callable task = new Daothread (sessionfacotry,sql,endsin
GLE);
Commit to thread pool future = Pool.submit (Task);
The results were put in Resultmap resultmap.put (tablename, future);
The thread pool closes, does not accept new tasks, executes only committed tasks Pool.shutdown ();
Wait for all threads to finish executing try {endsingle.await ();
catch (Interruptedexception E1) {e1.printstacktrace ();
return resultmap;
}
}
To execute a child thread:
* * @param threalocal thread to hibernate session/public class Daothread implements callable{private Sessionfactory
Sessionfacotry;
Private Countdownlatch Endsingle;
Creates a thread local variable that is used to save Hibernate session private static final ThreadLocal ThreadLocal = new ThreadLocal ();
Public Sessionfactory Getsessionfacotry () {return sessionfacotry;
public void Setsessionfacotry (Sessionfactory sessionfacotry) {this.sessionfacotry = Sessionfacotry;
} private String SQL;
Public String GetSQL () {return SQL;
Private Daothread () {} public Daothread (Sessionfactory sessionfacotry, String sql,countdownlatch endsingle) {
Super ();
This.sessionfacotry = Sessionfacotry;
This.sql = SQL;
This.endsingle=endsingle; @Override public list<?> Call () throws Exception {//threadlocal Gets the variable copy of each child thread session session = (session)
Threadlocal.get (); If it is the thread that first accesses the database (Session==null) gets a session from Sessionfactory//sessionfactory is thread-safe if (session = null) {
Session = This.sessionFacotry.openSession ();
Bind the hibernate session to the current thread Threadlocal.set (session);
Query query = session.createsqlquery (this.sql.toString ());
list<string[]> list = (list<string[]>) query.list ();
Session.close ();
Threadlocal.set (NULL);
if (list = = NULL | | list.size () ==0) {System.out.println ("No Validate Entry");
Thread counter minus one endsingle.countdown ();
return null;
}//Thread counter minus one endsingle.countdown ();
return list;
}
}
Result set Processing:
The return value type in the result set is future, obtained using get ().
iterator = Resultmap.keyset (). iterator ();
while (Iterator.hasnext ()) {
string key = (String) iterator.next ();
future<?> f = (future<?>) resultmap.get (key);
List lst = null;;
try {
lst = (List) f.get ();
} catch (Interruptedexception e) {
e.printstacktrace ();
} catch ( Executionexception e) {
e.printstacktrace ();
}
Check the list
System.out.println (key+ ":" +lst) returned from hibernate;
if (LST!= null) {for
(int i=0;i<lst.size (); i++) {
object[] row= (object[)) lst.get (i);
System.out.println ("row[0]=" +row[0].tostring ());
System.out.println ("row[1]=" +row[1].tostring ());
System.out.println ("row[2]=" +row[2].tostring ());}}}