Project

General

Profile

« Previous | Next » 

Revision 471

Added by bojilova over 23 years ago

Separate thread used for writing into xml_index table.
This cut the time of the response on insert almost in half.
The tread is started afterwards
(on end of document parsing and inserting into xml_nodes) from
DBSAXHandler and uses new(separate) db connection.
But I did not find a good way to show the work done of that thread.

View differences:

DBSAXHandler.java
16 16

  
17 17
import java.sql.*;
18 18
import java.util.Stack;
19
import java.util.Vector;
20
import java.util.Enumeration;
19 21
import java.util.EmptyStackException;
20 22

  
21 23
import org.xml.sax.Attributes;
......
30 32
 * call when processing the XML stream and generating events
31 33
 */
32 34
public class DBSAXHandler extends DefaultHandler 
33
                          implements LexicalHandler, DeclHandler {
35
                          implements LexicalHandler, DeclHandler, Runnable {
34 36

  
35 37
   private boolean	atFirstElement;
36 38
   private boolean	processingDTD;
......
38 40
   private String 	doctype;
39 41
   private String 	systemid;
40 42
   private boolean 	stackCreated = false;
41
   private Stack 	nodeStack;
42
   private Connection	conn = null;
43
   private Stack 	  nodeStack;
44
   private Vector   nodeIndex;
45
   private Connection	  conn = null;
43 46
   private DocumentImpl currentDocument;
44 47
   private DBSAXNode    rootNode;
45
   private String       action = null;
46
   private String       docid = null;
47
   private String       user = null;
48
   private String   action = null;
49
   private String   docid = null;
50
   private String   user = null;
51
   private Thread   xmlIndex;
52
   private boolean endDocument = false;
48 53

  
49 54
   private static final int MAXDATACHARS = 4000;
50 55
   private static final int MAXTITLELEN = 1000;
......
62 67
     // if it doesn't already exist
63 68
     if (!stackCreated) {
64 69
       nodeStack = new Stack();
70
       nodeIndex = new Vector();
65 71
       stackCreated = true;
66 72
     }
67 73
   }
......
79 85
     this.action = action;
80 86
     this.docid = docid;
81 87
     this.user = user;
88
     this.xmlIndex = new Thread(this);
89
     //this.xmlIndex.setPriority(Thread.MIN_PRIORITY);
82 90
   }
83 91

  
84 92
   /** SAX Handler that receives notification of beginning of the document */
......
94 102

  
95 103
   /** SAX Handler that receives notification of end of the document */
96 104
   public void endDocument() throws SAXException {
97
// NOT NEEDED ANY MORE - use currentDocument.setTitle() from characters(cbuf,start,end)
98
//     currentDocument.setTitleFromChildElement();
99 105
     MetaCatUtil.debugMessage("end Document");
100
// NOT NEEDED ANY MORE - Accession# is generated at the very beginning before the parsing
101
//     if ((docid != null) && (!docid.equals(currentDocument.getDocID()))) {
102
//       throw (new SAXException("New document ID generated:",
103
//           new AccessionNumberGeneratedException(currentDocument.getDocID())));
104
//     } else {
105
//       throw (new SAXException("New document ID generated:",
106
//           new AccessionNumberGeneratedException(currentDocument.getDocID())));
107
//     }
106
     // Starting new thread for writing XML Index.
107
     // It calls the run method of the thread.
108
     try {
109
       xmlIndex.start();
110
     } catch (NullPointerException e) {
111
       xmlIndex = null;
112
       throw new 
113
       SAXException("Problem with starting thread for writing XML Index. " +
114
                    e.getMessage());
115
     }
108 116
   }
109 117

  
110 118
   /** SAX Handler that is called at the start of each XML element */
......
151 159
     currentNode = new DBSAXNode(conn, localName, parentNode,
152 160
                                 currentDocument.getRootNodeID(),docid,
153 161
                                 currentDocument.getDoctype());
154
                                 
162
                               
155 163
     // Add all of the attributes
156 164
     for (int i=0; i<atts.getLength(); i++) {
157 165
       currentNode.setAttribute(atts.getLocalName(i), atts.getValue(i), docid);
......
160 168
     // Add the node to the stack, so that any text data can be 
161 169
     // added as it is encountered
162 170
     nodeStack.push(currentNode);
171
     // Add the node to the vector used by thread for writing XML Index
172
     nodeIndex.addElement(currentNode);
173

  
163 174
  }
175
  
176
  // The run method of xmlIndex thread. It writes XML Index for the document.
177
  public void run () {
178
    DBSAXNode currNode = null;
179
    DBSAXNode prevNode = null;
180
    int step = 0;
181
    int counter = 0;
164 182

  
183
    try {
184
      // Opening separate db connection for writing XML Index
185
      MetaCatUtil util = new MetaCatUtil();
186
      Connection conn = util.getConnection();
187
      conn.setAutoCommit(false);
188

  
189
      // Going through the elements of the document and writing its Index
190
      Enumeration nodes = nodeIndex.elements();
191
      while ( nodes.hasMoreElements() ) {
192
        currNode = (DBSAXNode)nodes.nextElement();
193
        currNode.updateNodeIndex(conn, docid, currentDocument.getDoctype());
194
      }
195
    
196
      conn.commit();
197
      conn.close();
198
    
199
    } catch (Exception e) {
200
      try {
201
        conn.rollback();
202
        conn.close();
203
      } catch (SQLException sqle) {}
204
      System.out.println("Error writing XML Index. " + e.getMessage()); 
205
    }      
206
  }
207

  
165 208
  /** SAX Handler that is called for each XML text node */
166 209
  public void characters(char[] cbuf, int start, int len) throws SAXException {
167 210
     MetaCatUtil.debugMessage("CHARACTERS");

Also available in: Unified diff