Project

General

Profile

« Previous | Next » 

Revision 797

Added by bojilova almost 23 years ago

Simplified ReplicationHandler:
It is run now in the same thread started from DBSAXHandler;
thus the same connection opened in that thread is used,
i.e. no need for new connection and no complains as "Connection is closed" in ReplicationHandler;
"Connection is closed" error happens when in the current thread there are try to use connection opened in another thread,
i.e. DON'T USE SAME CONNECTION BETWEEN DIFFERENT THREADS.

View differences:

src/edu/ucsb/nceas/metacat/DBSAXHandler.java
256 256
      //if it is a package file so you don't have to do it here.
257 257
      if ( doctype.equals(util.getOption("packagedoctype")) )
258 258
      {
259
        DocumentImpl xmldoc = new DocumentImpl(dbconn, docid);
260
        RelationHandler rth = new RelationHandler(xmldoc, dbconn);
259
        //DocumentImpl xmldoc = new DocumentImpl(dbconn, docid);
260
        RelationHandler rth = new RelationHandler(docid, dbconn);
261 261
      } 
262 262
      else if ( doctype.equals(util.getOption("accessdoctype")) ) 
263 263
      {
src/edu/ucsb/nceas/metacat/RelationHandler.java
22 22

  
23 23
import java.sql.*;
24 24
import java.util.*;
25
import java.lang.Thread; 
26 25
import java.net.*;
27 26

  
28
public class RelationHandler implements Runnable
27
public class RelationHandler //implements Runnable
29 28
{
30 29
  private Thread btThread = null;
31 30
  private Connection conn = null;
32
  private DocumentImpl xmldoc = null;
31
  private String docid = null;
33 32
  MetaCatUtil util = new MetaCatUtil();
34 33
  
35 34
  /** 
36 35
   * Constructor for this class.  finds all of the relations to a single xml
37 36
   * document and writes them to the database.  This includes transitive
38 37
   * relations.
39
   * @param xmldoc the xml document to index.
38
   * @param docid the ID of the XML document to index.
40 39
   */
41
  public RelationHandler(DocumentImpl xmldoc, Connection conn)
40
  public RelationHandler(String docid, Connection conn)
42 41
  {
43
    //this.conn = conn;
44
    try
45
    {
46
      conn = MetacatReplication.getDBConnection("RelationHandler." +
47
                                                "relationHandler");
48
    }
49
    catch(Exception e)
50
    {
51
      MetaCatUtil.debugMessage("unable to get db connection in " + 
52
                         "relationhandler: " + e.getMessage());
53
    }
54
    this.xmldoc = xmldoc;
55
    if(xmldoc.getDoctype().equals(util.getOption("packagedoctype")))
56
    { //make sure this doctype is a package document then run the thread
57
      btThread = new Thread(this);
58
      btThread.setPriority(Thread.MIN_PRIORITY);
59
      btThread.start();
60
    }
42
    this.conn = conn;
43
    this.docid = docid;
44
    putRelations();
61 45
  }
62 46
  
63 47
  /**
64
   * The thread handler
48
   * insert the relations specified in the triples into xml_relation table
65 49
   */ 
66
  public void run()
67
  {
50
  public void putRelations() {
51
    
68 52
    MetaCatUtil.debugMessage("Running relation handler!");
69
    Connection dbconn = null;
70
    String docid = xmldoc.getDocID();
71
    String packagetype = xmldoc.getDoctype();
53

  
72 54
    // deletes all of the relations with a docid of @docid.
73
    deleteRelations(docid);
74 55
    //pseudo-code algorithm
75 56
    //for each new relation r in xml_nodes
76 57
    //  put r into xml_relation
77 58
    //  compare r to each relation already in xml_relation
78 59
    //  if r isrelatedto a row in xml_relation
79 60
    //    add a new row to xml_relation that represents this new relation
80
    try
81
    {
82
      dbconn = MetacatReplication.getDBConnection("RelationHandler." +
83
                                                  "run");
61
    try {
84 62

  
85
      PreparedStatement pstmt = dbconn.prepareStatement(
63
      DocumentImpl xmldoc = new DocumentImpl(conn);
64
      xmldoc.getDocumentInfo(docid);
65
      String packagetype = xmldoc.getDoctype();
66

  
67
      // first delete the relations for this package document if any
68
      deleteRelations(docid);
69
      // put the new relations
70
      PreparedStatement pstmt = conn.prepareStatement(
86 71
                                QuerySpecification.printPackageSQL(docid));
87 72
      pstmt.execute();
88 73
      
89 74
      //get the new relations out of xml_nodes
90 75
      ResultSet rs = pstmt.getResultSet();
91 76
      boolean hasmorerows = rs.next();
92
      while(hasmorerows)
93
      {
77
      while(hasmorerows) {
94 78
        String subject = rs.getString(1);
95 79
        String relationship = rs.getString(2);
96 80
        String object = rs.getString(3);
......
105 89
        String subDoctype = null;
106 90
        String objDoctype = null;
107 91
        
108
        try
109
        {
92
        try {
110 93
          URL subMurl = new URL(subject);
111
          if(subMurl.getQuery() != null)
112
          {
94
          if(subMurl.getQuery() != null) {
113 95
            Hashtable subMurlParams = util.parseQuery(subMurl.getQuery());
114 96
            subDocid = (String)subMurlParams.get("docid");
115
            if(subMurl.getProtocol().equals("metacat"))
116
            {
117
              DocumentImpl subDoc = new DocumentImpl(dbconn, subDocid);
97
            if(subMurl.getProtocol().equals("metacat")) {
98
              DocumentImpl subDoc = new DocumentImpl(conn, subDocid);
118 99
              subDoctype = subDoc.getDoctype();
119 100
            }
120
          }
121
          else
122
          {
101
          } else {
123 102
            subDocid = subject;
124 103
          }
125
        }
126
        catch(MalformedURLException murle)
127
        { //assume this is just a docid not a url
104
        } catch(MalformedURLException murle) { 
105
          //assume this is just a docid not a url
128 106
          subDocid = subject;
129 107
        }
130 108
        
131
        try
132
        {
109
        try {
133 110
          URL objMurl = new URL(object); 
134
          if(objMurl.getQuery() != null)
135
          {
111
          if(objMurl.getQuery() != null) {
136 112
            Hashtable objMurlParams = util.parseQuery(objMurl.getQuery());
137 113
            objDocid = (String)objMurlParams.get("docid");
138
            if(objMurl.getProtocol().equals("metacat"))
139
            {
140
              DocumentImpl objDoc = new DocumentImpl(dbconn, objDocid);
114
            if(objMurl.getProtocol().equals("metacat")) {
115
              DocumentImpl objDoc = new DocumentImpl(conn, objDocid);
141 116
              objDoctype = objDoc.getDoctype();
142 117
            }
143
          }
144
          else
145
          {
118
          } else {
146 119
            objDocid = object;
147 120
          }
148
        }
149
        catch(MalformedURLException murle)
150
        { //assume this is just a docid
121
        } catch(MalformedURLException murle) {
122
          //assume this is just a docid
151 123
          objDocid = object;
152 124
        }
153 125
        
......
166 138
        insertStmt.append(object).append("', '");
167 139
        insertStmt.append(objDoctype).append("')");
168 140
        
169
        pstmt = dbconn.prepareStatement(insertStmt.toString());
141
        pstmt = conn.prepareStatement(insertStmt.toString());
170 142
        pstmt.execute(); 
171 143
        
172 144
        hasmorerows = rs.next();
173 145
      }
174 146
      
175
      dbconn.commit();
176 147
      pstmt.close();
177
      dbconn.close();
178
      btThread = null;
148
      conn.commit();
179 149

  
180
    } 
181
    catch(Exception e) 
182
    { 
183
      MetaCatUtil.debugMessage("Error in relationHandler.run: " + 
150
    } catch(Exception e) { 
151
      MetaCatUtil.debugMessage("Error in RelationHandler.run(): " + 
184 152
                               e.getMessage());
185
      btThread = null;
186
      try 
187
      { 
153
      System.out.println("Error in RelationHandler.run(): " + 
154
                               e.getMessage());
155
      try { 
188 156
        conn.rollback();
189
      } 
190
      catch (SQLException sqle) {}
157
      } catch (SQLException sqle) {
158
        System.out.println("Error in RelationHandler.run(): " + 
159
                           sqle.getMessage());
160
      }
191 161
    }
192 162
  }
193 163
  
......
195 165
   * Deletes all of the relations with a docid of 'docid'.
196 166
   * @param docid the docid to delete.
197 167
   */
198
  public void deleteRelations(String docid)
168
  public void deleteRelations(String docid) throws SQLException
199 169
  {
200
    Connection dbconn = null;
201
    try
202
    {
203
      dbconn = MetacatReplication.getDBConnection("RelationHandler."+
204
                                                  "deleteRelations");
205
    }
206
    catch(Exception ee)
207
    {
208
      MetaCatUtil.debugMessage("error in relationHandler.deleteRelations: " +
209
                          ee.getMessage());
210
    }
211
    
212
    try
213
    {
214
      PreparedStatement pstmt = dbconn.prepareStatement("delete from " +
170
    try {
171
      PreparedStatement pstmt = conn.prepareStatement("delete from " +
215 172
                             "xml_relation where docid like '" + docid + "'");
216 173
      pstmt.execute();
217 174
      pstmt.close();
218
      dbconn.close();
219
    }
220
    catch(Exception e)
221
    {
175
    } catch(SQLException e) {
222 176
      MetaCatUtil.debugMessage("error in RelationHandler.deleteRelations(): " + 
223 177
                          e.getMessage());
224
      try 
225
      { 
226
        dbconn.rollback();
227
        dbconn.close();
228
      } 
229
      catch (SQLException sqle) {}
178
      System.out.println("error in RelationHandler.deleteRelations(): " + 
179
                          e.getMessage());
180
      throw e;
230 181
    }
231 182
  }
232 183
}

Also available in: Unified diff