Revision 30928
Added by Sandro La Bruzzo over 9 years ago
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreTransactionManagerImpl.java | ||
---|---|---|
125 | 125 |
* @throws MDStoreServiceException |
126 | 126 |
* the MD store service exception |
127 | 127 |
*/ |
128 |
private void verifyConsistency() throws MDStoreServiceException { |
|
128 |
@Override |
|
129 |
public void verifyConsistency() throws MDStoreServiceException { |
|
129 | 130 |
if (this.getManagerTable() == null) { |
130 | 131 |
if (!db.collectionExists(TABLE_NAME)) { |
131 | 132 |
bootstrapManager(); |
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/EnsureIndexJob.java | ||
---|---|---|
4 | 4 |
import org.apache.commons.logging.LogFactory; |
5 | 5 |
import org.springframework.beans.factory.annotation.Required; |
6 | 6 |
|
7 |
import eu.dnetlib.data.mdstore.MDStoreServiceException; |
|
7 | 8 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao; |
8 | 9 |
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore; |
9 | 10 |
import eu.dnetlib.enabling.tools.AbstractSchedulable; |
... | ... | |
18 | 19 |
protected void doExecute() { |
19 | 20 |
log.info("performing mdstore index check"); |
20 | 21 |
|
21 |
for (String mdId : getDao().listMDStores()) { |
|
22 |
try { |
|
23 |
log.info("ensureindex for mdStoreId:" + mdId); |
|
24 |
((MongoMDStore) getDao().getMDStore(mdId)).ensureIndices(); |
|
25 |
} catch (Throwable e) { |
|
26 |
log.warn("unable to reindex mdstore: " + mdId, e); |
|
22 |
try { |
|
23 |
for (String mdId : getDao().listMDStores()) { |
|
24 |
try { |
|
25 |
log.info("ensureindex for mdStoreId:" + mdId); |
|
26 |
((MongoMDStore) getDao().getMDStore(mdId)).ensureIndices(); |
|
27 |
} catch (Throwable e) { |
|
28 |
log.warn("unable to reindex mdstore: " + mdId, e); |
|
29 |
} |
|
27 | 30 |
} |
31 |
} catch (MDStoreServiceException e) { |
|
32 |
log.warn("unable to reindex mdstore ", e); |
|
28 | 33 |
} |
29 | 34 |
|
30 | 35 |
log.info("mdstore index check completed"); |
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/MetadataCheckJob.java | ||
---|---|---|
16 | 16 |
import com.mongodb.DBCollection; |
17 | 17 |
import com.mongodb.DBObject; |
18 | 18 |
|
19 |
import eu.dnetlib.data.mdstore.MDStoreServiceException; |
|
19 | 20 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao; |
20 | 21 |
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore; |
21 | 22 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
... | ... | |
31 | 32 |
* lookup locator. |
32 | 33 |
*/ |
33 | 34 |
private ServiceLocator<ISLookUpService> lookupLocator; |
34 |
|
|
35 |
|
|
35 | 36 |
/** |
36 | 37 |
* {@link Endpoint} |
37 | 38 |
*/ |
38 | 39 |
private Endpoint endpoint; |
39 |
|
|
40 |
|
|
40 | 41 |
/** |
41 | 42 |
* {@link EndpointReferenceBuilder} |
42 | 43 |
*/ |
43 |
private EndpointReferenceBuilder<Endpoint> eprBuilder;
|
|
44 |
|
|
44 |
private EndpointReferenceBuilder<Endpoint> eprBuilder; |
|
45 |
|
|
45 | 46 |
/** |
46 | 47 |
* MDStore dao. |
47 | 48 |
*/ |
48 | 49 |
private MDStoreDao dao; |
49 | 50 |
|
50 | 51 |
private boolean runOnStart; |
51 |
|
|
52 |
public void runOnStart() { |
|
53 |
if(isRunOnStart()) { |
|
52 |
|
|
53 |
public void runOnStart() throws MDStoreServiceException {
|
|
54 |
if (isRunOnStart()) {
|
|
54 | 55 |
log.info("running mdStore metadata check on service start"); |
55 | 56 |
repairMetadata(); |
56 | 57 |
} |
... | ... | |
58 | 59 |
|
59 | 60 |
/** |
60 | 61 |
* Job execution method. |
62 |
* |
|
63 |
* @throws MDStoreServiceException |
|
61 | 64 |
*/ |
62 |
public void repairMetadata() { |
|
65 |
public void repairMetadata() throws MDStoreServiceException {
|
|
63 | 66 |
DBCollection metadata = ((MongoMDStore) getDao().getMDStore("metadata")).getCollection(); |
64 | 67 |
if (metadata.findOne() != null) { |
65 | 68 |
log.debug("mdStore metadata doesn't need to be repaired"); |
... | ... | |
67 | 70 |
} |
68 | 71 |
try { |
69 | 72 |
List<String> mdStores = getLookupLocator().getService().quickSearchProfile( |
70 |
"//RESOURCE_PROFILE[" + |
|
71 |
".//RESOURCE_TYPE/@value='MDStoreDSResourceType' and " + |
|
72 |
".//RESOURCE_URI/@value='" + getServiceAddress() + "']"); |
|
73 |
"//RESOURCE_PROFILE[" + ".//RESOURCE_TYPE/@value='MDStoreDSResourceType' and " + ".//RESOURCE_URI/@value='" + getServiceAddress() + "']"); |
|
73 | 74 |
|
74 | 75 |
log.debug("repairing mdstore metadata"); |
75 |
|
|
76 |
|
|
77 |
if(mdStores!=null){ |
|
78 |
for(String MDStoreProfile : mdStores) |
|
76 |
|
|
77 |
if (mdStores != null) { |
|
78 |
for (String MDStoreProfile : mdStores) { |
|
79 | 79 |
metadata.save(getMdInfo(MDStoreProfile)); |
80 |
} |
|
80 | 81 |
} |
81 |
|
|
82 |
|
|
82 | 83 |
log.debug("FINISHED repairing mdstore metadata"); |
83 | 84 |
|
84 |
} catch(ISLookUpException e) { |
|
85 |
} catch (ISLookUpException e) {
|
|
85 | 86 |
throw new RuntimeException(e); |
86 |
} catch(DocumentException e ) {
|
|
87 |
} catch (DocumentException e) {
|
|
87 | 88 |
throw new RuntimeException(e); |
88 |
}
|
|
89 |
} |
|
89 | 90 |
} |
90 |
|
|
91 |
|
|
91 | 92 |
/** |
92 | 93 |
* Helper method, gets an MDStore profile and returns a DBObject. |
93 |
*
|
|
94 |
* |
|
94 | 95 |
* @param MDStoreProfile |
95 | 96 |
* @return a DBObject representing the metadata informations |
96 | 97 |
* @throws DocumentException |
... | ... | |
106 | 107 |
|
107 | 108 |
return dbo; |
108 | 109 |
} |
109 |
|
|
110 |
|
|
110 | 111 |
private String getServiceAddress() { |
111 | 112 |
return getEprBuilder().getAddress(getEndpoint()) + "?wsdl"; |
112 |
}
|
|
113 |
} |
|
113 | 114 |
|
114 | 115 |
@Required |
115 |
public void setEprBuilder(EndpointReferenceBuilder<Endpoint> eprBuilder) { |
|
116 |
public void setEprBuilder(final EndpointReferenceBuilder<Endpoint> eprBuilder) {
|
|
116 | 117 |
this.eprBuilder = eprBuilder; |
117 | 118 |
} |
118 | 119 |
|
119 | 120 |
public EndpointReferenceBuilder<Endpoint> getEprBuilder() { |
120 | 121 |
return eprBuilder; |
121 | 122 |
} |
122 |
|
|
123 |
|
|
123 | 124 |
@Required |
124 |
public void setEndpoint(Endpoint endpoint) { |
|
125 |
public void setEndpoint(final Endpoint endpoint) {
|
|
125 | 126 |
this.endpoint = endpoint; |
126 | 127 |
} |
127 | 128 |
|
128 | 129 |
public Endpoint getEndpoint() { |
129 | 130 |
return endpoint; |
130 |
}
|
|
131 |
} |
|
131 | 132 |
|
132 | 133 |
@Required |
133 |
public void setDao(MDStoreDao dao) { |
|
134 |
public void setDao(final MDStoreDao dao) {
|
|
134 | 135 |
this.dao = dao; |
135 | 136 |
} |
136 | 137 |
|
... | ... | |
139 | 140 |
} |
140 | 141 |
|
141 | 142 |
@Required |
142 |
public void setLookupLocator(ServiceLocator<ISLookUpService> lookupLocator) { |
|
143 |
public void setLookupLocator(final ServiceLocator<ISLookUpService> lookupLocator) {
|
|
143 | 144 |
this.lookupLocator = lookupLocator; |
144 | 145 |
} |
145 | 146 |
|
... | ... | |
148 | 149 |
} |
149 | 150 |
|
150 | 151 |
@Required |
151 |
public void setRunOnStart(boolean runOnStart) { |
|
152 |
public void setRunOnStart(final boolean runOnStart) {
|
|
152 | 153 |
this.runOnStart = runOnStart; |
153 | 154 |
} |
154 | 155 |
|
... | ... | |
157 | 158 |
} |
158 | 159 |
|
159 | 160 |
} |
160 |
|
modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/inspector/MDStoreInspector.java | ||
---|---|---|
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
public String getFormat() { |
67 |
return mdstoreDao.getMDStore(id).getFormat(); |
|
67 |
try { |
|
68 |
return mdstoreDao.getMDStore(id).getFormat(); |
|
69 |
} catch (MDStoreServiceException e) { |
|
70 |
return null; |
|
71 |
} |
|
68 | 72 |
} |
69 | 73 |
|
70 | 74 |
public String getLayout() { |
71 |
return mdstoreDao.getMDStore(id).getLayout(); |
|
75 |
try { |
|
76 |
return mdstoreDao.getMDStore(id).getLayout(); |
|
77 |
} catch (MDStoreServiceException e) { |
|
78 |
return null; |
|
79 |
} |
|
72 | 80 |
} |
73 | 81 |
|
74 | 82 |
public String getInterpretation() { |
75 |
return mdstoreDao.getMDStore(id).getInterpretation(); |
|
83 |
try { |
|
84 |
return mdstoreDao.getMDStore(id).getInterpretation(); |
|
85 |
} catch (MDStoreServiceException e) { |
|
86 |
return null; |
|
87 |
} |
|
76 | 88 |
} |
77 | 89 |
|
78 | 90 |
public boolean getIndexed() { |
... | ... | |
105 | 117 |
} |
106 | 118 |
|
107 | 119 |
@RequestMapping(value = "/inspector/mdstores.do") |
108 |
public void mdstores(final Model model) { |
|
120 |
public void mdstores(final Model model) throws MDStoreServiceException {
|
|
109 | 121 |
model.addAttribute("mdstores", MappedCollection.listMap(mdstoreDao.listMDStores(), new UnaryFunction<MDStoreDescription, String>() { |
110 | 122 |
|
111 | 123 |
@Override |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/DeleteAction.java | ||
---|---|---|
21 | 21 |
private MDStoreTransactionManager transactionManager; |
22 | 22 |
|
23 | 23 |
@Override |
24 |
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws ISRegistryDocumentNotFoundException, ISRegistryException { |
|
24 |
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws ISRegistryDocumentNotFoundException, ISRegistryException, |
|
25 |
MDStoreServiceException { |
|
25 | 26 |
registryLocator.getService().deleteProfile(job.getParameters().get("id")); |
26 | 27 |
String currentId = job.getParameters().get("id"); |
27 | 28 |
try { |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/CreateAction.java | ||
---|---|---|
18 | 18 |
private MDStoreTransactionManager transactionManager; |
19 | 19 |
|
20 | 20 |
@Override |
21 |
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws ISRegistryException { |
|
21 |
public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws ISRegistryException, MDStoreServiceException {
|
|
22 | 22 |
final String format = job.getParameters().get("format"); |
23 | 23 |
final String interpretation = job.getParameters().get("interpretation"); |
24 | 24 |
final String layout = job.getParameters().get("layout"); |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/MDStoreRetriever.java | ||
---|---|---|
38 | 38 |
|
39 | 39 |
} |
40 | 40 |
|
41 |
public Iterable<String> deliver(final String format, final String layout, final String interpretation) { |
|
41 |
public Iterable<String> deliver(final String format, final String layout, final String interpretation) throws MDStoreServiceException {
|
|
42 | 42 |
return Iterables.concat(Iterables.transform(dao.listMDStores(format, layout, interpretation), new Function<String, Iterable<String>>() { |
43 | 43 |
|
44 | 44 |
@Override |
... | ... | |
81 | 81 |
return resultSetFactory; |
82 | 82 |
} |
83 | 83 |
|
84 |
public void verifyConsistency() throws MDStoreServiceException { |
|
85 |
transactionManager.verifyConsistency(); |
|
86 |
} |
|
87 |
|
|
84 | 88 |
} |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/connector/MDStoreDao.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.List; |
4 | 4 |
|
5 |
import eu.dnetlib.data.mdstore.MDStoreServiceException; |
|
6 |
|
|
5 | 7 |
public interface MDStoreDao { |
6 | 8 |
|
7 |
MDStore getMDStore(String mdId); |
|
9 |
MDStore getMDStore(String mdId) throws MDStoreServiceException;
|
|
8 | 10 |
|
9 |
List<String> listMDStores(); |
|
11 |
List<String> listMDStores() throws MDStoreServiceException;
|
|
10 | 12 |
|
11 |
List<String> listMDStores(String format, String layout, String interpretation); |
|
13 |
List<String> listMDStores(String format, String layout, String interpretation) throws MDStoreServiceException;
|
|
12 | 14 |
|
13 |
void createMDStore(String mdId, String format, String interpretation, String layout); |
|
15 |
void createMDStore(String mdId, String format, String interpretation, String layout) throws MDStoreServiceException;
|
|
14 | 16 |
|
15 |
void deleteMDStore(String id); |
|
17 |
void deleteMDStore(String id) throws MDStoreServiceException;
|
|
16 | 18 |
} |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/connector/MDStoreTransactionManager.java | ||
---|---|---|
5 | 5 |
public interface MDStoreTransactionManager { |
6 | 6 |
|
7 | 7 |
/** |
8 |
* Verify consistency. |
|
9 |
* |
|
10 |
* @throws MDStoreServiceException |
|
11 |
* the MD store service exception |
|
12 |
*/ |
|
13 |
public abstract void verifyConsistency() throws MDStoreServiceException; |
|
14 |
|
|
15 |
/** |
|
8 | 16 |
* Creates the md store. |
9 |
*
|
|
17 |
* |
|
10 | 18 |
* @param mdId |
11 | 19 |
* the md id |
12 | 20 |
* @throws MDStoreServiceException |
... | ... | |
16 | 24 |
|
17 | 25 |
/** |
18 | 26 |
* Drop md store. |
19 |
*
|
|
27 |
* |
|
20 | 28 |
* @param mdId |
21 | 29 |
* the md id |
22 | 30 |
* @throws MDStoreServiceException |
... | ... | |
26 | 34 |
|
27 | 35 |
/** |
28 | 36 |
* Gets the MD store collection. |
29 |
*
|
|
37 |
* |
|
30 | 38 |
* @param mdId |
31 | 39 |
* the md id |
32 | 40 |
* @return the MD store collection |
... | ... | |
38 | 46 |
/** |
39 | 47 |
* Start a new transaction for writing in the mdstore this will create a temporary mdstore in which save the content. and after finished |
40 | 48 |
* switch it to the existing one |
41 |
*
|
|
49 |
* |
|
42 | 50 |
* @param mdId |
43 | 51 |
* @param refresh |
44 | 52 |
* @throws MDStoreServiceException |
... | ... | |
48 | 56 |
|
49 | 57 |
/** |
50 | 58 |
* Commit the transaction |
51 |
*
|
|
52 |
*
|
|
59 |
* |
|
60 |
* |
|
53 | 61 |
* @param transactionId |
54 | 62 |
* @param mdId |
55 | 63 |
* @return |
... | ... | |
60 | 68 |
/** |
61 | 69 |
* Book a a current mdstore, so the transaction manager can't delete during a possible commit, until the resultset associate has not |
62 | 70 |
* expired |
63 |
*
|
|
71 |
* |
|
64 | 72 |
* @param mdStoreId |
65 | 73 |
* @return |
66 | 74 |
* @throws MDStoreServiceException |
... | ... | |
69 | 77 |
|
70 | 78 |
/** |
71 | 79 |
* Return a JSON about the transaction assigned to a particular mdstore |
72 |
*
|
|
80 |
* |
|
73 | 81 |
* @param mdStoreId |
74 | 82 |
* @return |
75 | 83 |
* @throws MDStoreServiceException |
... | ... | |
78 | 86 |
|
79 | 87 |
/** |
80 | 88 |
* Manually drop an old collection assigned to a particular mdStore |
81 |
*
|
|
89 |
* |
|
82 | 90 |
* @param mdId |
83 | 91 |
* : the id of the mdStore |
84 | 92 |
* @param idToDrop |
... | ... | |
90 | 98 |
|
91 | 99 |
/** |
92 | 100 |
* Manually drop an old collection assigned to a particular mdStore |
93 |
*
|
|
101 |
* |
|
94 | 102 |
* @param mdId |
95 | 103 |
* : the id of the mdStore |
96 | 104 |
* @param idToDrop |
... | ... | |
102 | 110 |
|
103 | 111 |
/** |
104 | 112 |
* Start the garbage collection of the old mdstore not used |
105 |
*
|
|
113 |
* |
|
106 | 114 |
* @throws MDStoreServiceException |
107 | 115 |
*/ |
108 | 116 |
public abstract void garbage() throws MDStoreServiceException; |
modules/cnr-modular-mdstore-service/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/ModularMDStoreService.java | ||
---|---|---|
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
@Override |
46 |
public void start() { |
|
47 |
try { |
|
48 |
retriever.getDao().listMDStores(); |
|
49 |
retriever.verifyConsistency(); |
|
50 |
} catch (MDStoreServiceException e) { |
|
51 |
|
|
52 |
} |
|
53 |
super.start(); |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
46 | 57 |
public List<String> getListOfMDStores() throws MDStoreServiceException { |
47 | 58 |
return retriever.getDao().listMDStores(); |
48 | 59 |
} |
modules/dnet-postgres-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/postgres/PostgresMDStore.java | ||
---|---|---|
24 | 24 |
*/ |
25 | 25 |
public class PostgresMDStore implements MDStore { |
26 | 26 |
|
27 |
private static final int MAX_NUM_RECORD_BEFORE_COMMIT = 10000;
|
|
27 |
private static final int MAX_NUM_RECORD_BEFORE_COMMIT = 100; |
|
28 | 28 |
|
29 | 29 |
/** The id. */ |
30 | 30 |
private String id; |
... | ... | |
125 | 125 |
*/ |
126 | 126 |
@Override |
127 | 127 |
public void truncate() { |
128 |
Connection conn = null; |
|
128 | 129 |
try { |
129 |
Connection conn = datasource.getConnection();
|
|
130 |
conn = datasource.getConnection(); |
|
130 | 131 |
// DELETE ALL ITEM IN FIELDS |
131 | 132 |
conn.setAutoCommit(false); |
132 | 133 |
String sql = String.format("delete from %s_fields", this.id); |
... | ... | |
152 | 153 |
} catch (SQLException e) { |
153 | 154 |
log.error("Erron on dropping table " + this.id); |
154 | 155 |
|
156 |
} finally { |
|
157 |
if (conn != null) { |
|
158 |
try { |
|
159 |
conn.close(); |
|
160 |
} catch (SQLException e) { |
|
161 |
log.error("Error on get mdstore", e); |
|
162 |
} |
|
163 |
} |
|
155 | 164 |
} |
156 | 165 |
|
157 | 166 |
} |
... | ... | |
170 | 179 |
final BlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(80); |
171 | 180 |
final Object sentinel = new Object(); |
172 | 181 |
Thread background = null; |
182 |
Connection connection = null; |
|
173 | 183 |
try { |
174 |
final Connection conn = this.datasource.getConnection(); |
|
184 |
connection = this.datasource.getConnection(); |
|
185 |
final Connection conn = connection; |
|
175 | 186 |
conn.setAutoCommit(false); |
176 | 187 |
// DROP DISCARDED COLLECTION AT NEW FEEDING |
177 | 188 |
String dropDiscardedCollectionQuery = String.format("delete from %s_discarded", this.id); |
... | ... | |
183 | 194 |
@Override |
184 | 195 |
public void run() { |
185 | 196 |
int commitNumber = 0; |
186 |
|
|
187 | 197 |
while (true) { |
188 | 198 |
try { |
189 | 199 |
Object record = queue.take(); |
190 | 200 |
if (record == sentinel) { |
191 | 201 |
conn.commit(); |
192 | 202 |
updateSizeOnMetadataTable(conn); |
203 |
if (conn != null) { |
|
204 |
try { |
|
205 |
conn.close(); |
|
206 |
} catch (SQLException e) { |
|
207 |
log.error("Error on close mdstore", e); |
|
208 |
} |
|
209 |
} |
|
193 | 210 |
break; |
194 | 211 |
} |
195 | 212 |
safeFeedRecord((String) record, incremental, conn); |
... | ... | |
199 | 216 |
} |
200 | 217 |
} catch (InterruptedException e) { |
201 | 218 |
log.fatal("got exception in background thread", e); |
219 |
if (conn != null) { |
|
220 |
try { |
|
221 |
conn.close(); |
|
222 |
} catch (SQLException e1) { |
|
223 |
log.error("Error on close mdstore", e); |
|
224 |
} |
|
225 |
} |
|
202 | 226 |
throw new IllegalStateException(e); |
227 |
|
|
203 | 228 |
} catch (SQLException e) { |
204 | 229 |
log.fatal("got exception in background thread", e); |
230 |
if (conn != null) { |
|
231 |
try { |
|
232 |
conn.close(); |
|
233 |
} catch (SQLException e1) { |
|
234 |
log.error("Error on close mdstore", e); |
|
235 |
} |
|
236 |
} |
|
205 | 237 |
throw new IllegalStateException(e); |
206 | 238 |
} |
207 | 239 |
} |
... | ... | |
210 | 242 |
}); |
211 | 243 |
background.start(); |
212 | 244 |
} catch (SQLException e) { |
245 |
|
|
246 |
if (connection != null) { |
|
247 |
try { |
|
248 |
connection.close(); |
|
249 |
} catch (SQLException e1) { |
|
250 |
log.error("Error on close mdstore", e); |
|
251 |
} |
|
252 |
} |
|
213 | 253 |
throw new IllegalStateException(e); |
214 | 254 |
} |
215 | 255 |
try { |
... | ... | |
269 | 309 |
|
270 | 310 |
private void feedRecord(final String record, final boolean incremental, final Connection connection) { |
271 | 311 |
final Map<String, String> recordProperties = getRecordParser().parseRecord(record); |
312 |
|
|
272 | 313 |
log.debug("found props: " + recordProperties); |
273 | 314 |
if (recordProperties.containsKey("id")) { |
274 | 315 |
String p = "WITH upsert AS (UPDATE %s SET body=?, timestampdate=? WHERE recordid=? RETURNING *) " |
... | ... | |
287 | 328 |
ps.executeUpdate(); |
288 | 329 |
|
289 | 330 |
} catch (SQLException e) { |
290 |
log.error("Error on insert Element"); |
|
331 |
log.error("Error on insert Element", e);
|
|
291 | 332 |
} |
292 | 333 |
} else { |
293 | 334 |
if (discardRecords) { |
... | ... | |
365 | 406 |
*/ |
366 | 407 |
@Override |
367 | 408 |
public void deleteRecord(final String recordId) { |
409 |
Connection connection = null; |
|
368 | 410 |
try { |
369 |
Connection connection = this.datasource.getConnection();
|
|
411 |
connection = this.datasource.getConnection(); |
|
370 | 412 |
String sql = String.format("delete from %s where recordid = ?", this.id); |
371 | 413 |
PreparedStatement ps = connection.prepareStatement(sql); |
372 | 414 |
ps.setString(1, recordId); |
373 | 415 |
ps.executeUpdate(); |
374 | 416 |
} catch (SQLException e) { |
375 | 417 |
log.error("ERROR ON DELETING record ", e); |
418 |
} finally { |
|
419 |
if (connection != null) { |
|
420 |
try { |
|
421 |
connection.close(); |
|
422 |
} catch (SQLException e) { |
|
423 |
log.error("Error on get mdstore", e); |
|
424 |
} |
|
425 |
} |
|
376 | 426 |
} |
377 | 427 |
} |
378 | 428 |
|
... | ... | |
387 | 437 |
*/ |
388 | 438 |
@Override |
389 | 439 |
public String getRecord(final String recordId) throws DocumentNotFoundException { |
440 |
Connection connection = null; |
|
390 | 441 |
try { |
391 |
Connection connection = this.datasource.getConnection();
|
|
442 |
connection = this.datasource.getConnection(); |
|
392 | 443 |
String sql = String.format("select body from %s where recordid = ?", this.id); |
393 | 444 |
PreparedStatement ps = connection.prepareStatement(sql); |
394 | 445 |
ps.setString(1, recordId); |
... | ... | |
400 | 451 |
} catch (SQLException e) { |
401 | 452 |
log.error("ERROR ON DELETING record ", e); |
402 | 453 |
return null; |
454 |
} finally { |
|
455 |
if (connection != null) { |
|
456 |
try { |
|
457 |
connection.close(); |
|
458 |
} catch (SQLException e) { |
|
459 |
log.error("Error on get mdstore", e); |
|
460 |
} |
|
461 |
} |
|
403 | 462 |
} |
404 | 463 |
} |
405 | 464 |
|
modules/dnet-postgres-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/postgres/PostgresResultSetListener.java | ||
---|---|---|
33 | 33 |
|
34 | 34 |
@Override |
35 | 35 |
public List<String> getResult(final int fromPosition, final int toPosition) { |
36 |
Connection connection = null; |
|
36 | 37 |
try { |
37 |
Connection connection = datasource.getConnection();
|
|
38 |
String sql = String.format("select %s from %s order by recordid limit %d offset %d", this.selectedField, this.mdid, (fromPosition - 1),
|
|
39 |
((toPosition - fromPosition) + 1)); |
|
38 |
connection = datasource.getConnection(); |
|
39 |
String sql = String.format("select %s from %s order by recordid limit %d offset %d", this.selectedField, this.mdid, |
|
40 |
((toPosition - fromPosition) + 1), (fromPosition - 1));
|
|
40 | 41 |
List<String> output = new ArrayList<String>(); |
41 | 42 |
PreparedStatement ps = connection.prepareStatement(sql); |
42 | 43 |
java.sql.ResultSet rs = ps.executeQuery(); |
... | ... | |
47 | 48 |
} catch (SQLException e) { |
48 | 49 |
log.error(e); |
49 | 50 |
return null; |
51 |
} finally { |
|
52 |
if (connection != null) { |
|
53 |
try { |
|
54 |
connection.close(); |
|
55 |
} catch (SQLException e) { |
|
56 |
log.error("Error on close connection", e); |
|
57 |
|
|
58 |
} |
|
59 |
} |
|
50 | 60 |
} |
51 | 61 |
|
52 | 62 |
} |
modules/dnet-postgres-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/postgres/PostgresTransactionManagerImpl.java | ||
---|---|---|
58 | 58 |
private Resource templateCollection; |
59 | 59 |
|
60 | 60 |
/** The datasource. */ |
61 |
@Autowired
|
|
61 |
@javax.annotation.Resource(name = "mdstoreDataSource")
|
|
62 | 62 |
private DataSource datasource; |
63 | 63 |
|
64 | 64 |
/** The mdstore dao. */ |
... | ... | |
98 | 98 |
* @throws MDStoreServiceException |
99 | 99 |
* the MD store service exception |
100 | 100 |
*/ |
101 |
private void verifyConsistency() throws MDStoreServiceException { |
|
101 |
@Override |
|
102 |
public void verifyConsistency() throws MDStoreServiceException { |
|
103 |
Connection connection = null; |
|
102 | 104 |
try { |
103 |
Connection connection = this.datasource.getConnection(); |
|
105 |
connection = this.datasource.getConnection(); |
|
106 |
if (!existsTable("metadata", connection)) { |
|
107 |
mdstoreDAO.listMDStores(); |
|
108 |
} |
|
104 | 109 |
if (!existsTable(TRANSACTION_TABLE, connection)) { |
105 | 110 |
log.debug(String.format("Table %s does not exist , Creating table", TRANSACTION_TABLE)); |
106 | 111 |
createTable(templateTransaction, connection, TRANSACTION_TABLE); |
... | ... | |
109 | 114 |
log.debug(String.format("Table %s does not exist , Creating table", READLOCK_TABLE)); |
110 | 115 |
createTable(templateReadLock, connection, READLOCK_TABLE); |
111 | 116 |
} |
117 |
|
|
112 | 118 |
} catch (SQLException e) { |
113 | 119 |
log.error("Error during verify consistency ", e); |
120 |
} finally { |
|
121 |
if (connection != null) { |
|
122 |
try { |
|
123 |
connection.close(); |
|
124 |
} catch (SQLException e) { |
|
125 |
log.error("Error on close connection", e); |
|
126 |
throw new MDStoreServiceException(e); |
|
127 |
} |
|
128 |
} |
|
114 | 129 |
} |
115 | 130 |
} |
116 | 131 |
|
... | ... | |
159 | 174 |
|
160 | 175 |
/* |
161 | 176 |
* (non-Javadoc) |
162 |
*
|
|
177 |
* |
|
163 | 178 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#createMDStore(java.lang.String) |
164 | 179 |
*/ |
165 | 180 |
@Override |
... | ... | |
169 | 184 |
|
170 | 185 |
/* |
171 | 186 |
* (non-Javadoc) |
172 |
*
|
|
187 |
* |
|
173 | 188 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#dropMDStore(java.lang.String) |
174 | 189 |
*/ |
175 | 190 |
@Override |
176 | 191 |
public void dropMDStore(final String mdId) throws MDStoreServiceException { |
192 |
Connection connection = null; |
|
177 | 193 |
try { |
178 |
Connection connection = this.datasource.getConnection();
|
|
194 |
connection = this.datasource.getConnection(); |
|
179 | 195 |
verifyConsistency(); |
180 | 196 |
connection.setAutoCommit(true); |
181 | 197 |
String sql = String.format("select current_table from %s where mdid = ?", METADATA_TABLE); |
... | ... | |
197 | 213 |
} catch (SQLException e) { |
198 | 214 |
log.error("ERROR ON DROP MDSTORE " + mdId, e); |
199 | 215 |
throw new MDStoreServiceException("ERROR ON DROP MDSTORE " + mdId, e); |
216 |
} finally { |
|
217 |
if (connection != null) { |
|
218 |
try { |
|
219 |
connection.close(); |
|
220 |
} catch (SQLException e) { |
|
221 |
log.error("Error on close connection", e); |
|
222 |
throw new MDStoreServiceException(e); |
|
223 |
} |
|
224 |
} |
|
200 | 225 |
} |
201 | 226 |
|
202 | 227 |
} |
203 | 228 |
|
204 | 229 |
/* |
205 | 230 |
* (non-Javadoc) |
206 |
*
|
|
231 |
* |
|
207 | 232 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#getMDStoreCollection(java.lang.String) |
208 | 233 |
*/ |
209 | 234 |
@Override |
210 | 235 |
public String getMDStoreCollection(final String mdId) throws MDStoreServiceException { |
236 |
Connection connection = null; |
|
211 | 237 |
try { |
238 |
connection = this.datasource.getConnection(); |
|
212 | 239 |
verifyConsistency(); |
213 |
Connection connection = this.datasource.getConnection(); |
|
214 | 240 |
String table = getCurrentTable(mdId, connection); |
215 | 241 |
if (!existsTable(table, connection)) |
216 | 242 |
throw new MDStoreServiceException(String.format("ERROR on GET MDSTORE: unable to find the current mdstore associated on %s", mdId)); |
... | ... | |
218 | 244 |
} catch (SQLException e) { |
219 | 245 |
log.error("ERROR ON GET MDSTORE " + mdId, e); |
220 | 246 |
throw new MDStoreServiceException("ERROR ON GET MDSTORE " + mdId, e); |
247 |
} finally { |
|
248 |
if (connection != null) { |
|
249 |
try { |
|
250 |
connection.close(); |
|
251 |
} catch (SQLException e) { |
|
252 |
log.error("Error on close connection", e); |
|
253 |
throw new MDStoreServiceException(e); |
|
254 |
} |
|
255 |
} |
|
221 | 256 |
} |
222 | 257 |
|
223 | 258 |
} |
... | ... | |
251 | 286 |
|
252 | 287 |
/* |
253 | 288 |
* (non-Javadoc) |
254 |
*
|
|
289 |
* |
|
255 | 290 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#startTransaction(java.lang.String, boolean) |
256 | 291 |
*/ |
257 | 292 |
@Override |
258 | 293 |
public String startTransaction(final String mdId, final boolean refresh) throws MDStoreServiceException { |
294 |
Connection connection = null; |
|
259 | 295 |
try { |
296 |
connection = this.datasource.getConnection(); |
|
260 | 297 |
verifyConsistency(); |
261 |
Connection connection = this.datasource.getConnection(); |
|
262 | 298 |
String table = getCurrentTable(mdId, connection); |
263 | 299 |
if (mdId.contains("_")) { |
264 | 300 |
table = StringUtils.substringBefore(mdId, "_").replace("-", "_"); |
... | ... | |
275 | 311 |
return table; |
276 | 312 |
} catch (SQLException e) { |
277 | 313 |
throw new MDStoreServiceException("Error on creating transaction ", e); |
314 |
} finally { |
|
315 |
if (connection != null) { |
|
316 |
try { |
|
317 |
connection.close(); |
|
318 |
} catch (SQLException e) { |
|
319 |
log.error("Error on close connection", e); |
|
320 |
throw new MDStoreServiceException(e); |
|
321 |
} |
|
322 |
} |
|
278 | 323 |
} |
324 |
|
|
279 | 325 |
} |
280 | 326 |
|
281 | 327 |
/** |
... | ... | |
288 | 334 |
* the MD store service exception |
289 | 335 |
*/ |
290 | 336 |
public String getMDStoreForTransaction(final String transactionId) throws MDStoreServiceException { |
337 |
Connection connection = null; |
|
291 | 338 |
try { |
339 |
connection = this.datasource.getConnection(); |
|
292 | 340 |
verifyConsistency(); |
293 | 341 |
String sql = String.format("select mdstoreid from %s where id=?", TRANSACTION_TABLE); |
294 |
Connection connection = this.datasource.getConnection(); |
|
342 |
|
|
295 | 343 |
PreparedStatement statement = connection.prepareStatement(sql); |
296 | 344 |
statement.setString(1, transactionId); |
297 | 345 |
ResultSet rs = statement.executeQuery(); |
... | ... | |
299 | 347 |
return rs.getString(1); |
300 | 348 |
} catch (SQLException e) { |
301 | 349 |
return null; |
350 |
} finally { |
|
351 |
if (connection != null) { |
|
352 |
try { |
|
353 |
connection.close(); |
|
354 |
} catch (SQLException e) { |
|
355 |
log.error("Error on close connection", e); |
|
356 |
throw new MDStoreServiceException(e); |
|
357 |
} |
|
358 |
} |
|
302 | 359 |
} |
303 | 360 |
} |
304 | 361 |
|
305 | 362 |
/* |
306 | 363 |
* (non-Javadoc) |
307 |
*
|
|
364 |
* |
|
308 | 365 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#commit(java.lang.String, java.lang.String) |
309 | 366 |
*/ |
310 | 367 |
@Override |
... | ... | |
338 | 395 |
* the MD store service exception |
339 | 396 |
*/ |
340 | 397 |
private void dropEntriesInTransaction(final String tableName, final String mdId) throws MDStoreServiceException { |
398 |
Connection connection = null; |
|
341 | 399 |
try { |
342 |
Connection connection = this.datasource.getConnection();
|
|
400 |
connection = this.datasource.getConnection(); |
|
343 | 401 |
String sql = String.format("delete from %s where id=? and mdstoreid=?", TRANSACTION_TABLE); |
344 | 402 |
PreparedStatement ps = connection.prepareStatement(sql); |
345 | 403 |
ps.setString(1, tableName); |
... | ... | |
347 | 405 |
ps.executeUpdate(); |
348 | 406 |
} catch (SQLException e) { |
349 | 407 |
throw new MDStoreServiceException("Error on deleting transaction table"); |
408 |
} finally { |
|
409 |
if (connection != null) { |
|
410 |
try { |
|
411 |
connection.close(); |
|
412 |
} catch (SQLException e) { |
|
413 |
log.error("Error on close connection", e); |
|
414 |
throw new MDStoreServiceException(e); |
|
415 |
} |
|
416 |
} |
|
350 | 417 |
} |
351 | 418 |
} |
352 | 419 |
|
... | ... | |
382 | 449 |
String sql_delete_fields = String.format("DROP table IF EXISTS %s_fields", tableName); |
383 | 450 |
String sql_delete_discarded = String.format("DROP table IF EXISTS %s_discarded", tableName); |
384 | 451 |
String sql_delete_table = String.format("DROP table IF EXISTS %s", tableName); |
452 |
Connection connection = null; |
|
385 | 453 |
try { |
386 |
Connection connection = this.datasource.getConnection();
|
|
454 |
connection = this.datasource.getConnection(); |
|
387 | 455 |
connection.prepareStatement(sql_delete_fields).executeUpdate(); |
388 | 456 |
connection.prepareStatement(sql_delete_discarded).executeUpdate(); |
389 | 457 |
connection.prepareStatement(sql_delete_table).executeUpdate(); |
390 | 458 |
|
391 | 459 |
} catch (SQLException e) { |
392 | 460 |
throw new MDStoreServiceException("error on drop old table ", e); |
461 |
} finally { |
|
462 |
if (connection != null) { |
|
463 |
try { |
|
464 |
connection.close(); |
|
465 |
} catch (SQLException e) { |
|
466 |
log.error("Error on close connection", e); |
|
467 |
throw new MDStoreServiceException(e); |
|
468 |
} |
|
469 |
} |
|
393 | 470 |
} |
394 | 471 |
} |
395 | 472 |
|
... | ... | |
403 | 480 |
* the MD store service exception |
404 | 481 |
*/ |
405 | 482 |
private String retreiveCurrentId(final String mdId) throws MDStoreServiceException { |
483 |
Connection connection = null; |
|
406 | 484 |
try { |
407 |
Connection connection = this.datasource.getConnection();
|
|
485 |
connection = this.datasource.getConnection(); |
|
408 | 486 |
String sql = String.format("select current_table from %s where mdid=?", METADATA_TABLE); |
409 | 487 |
PreparedStatement st = connection.prepareStatement(sql); |
410 | 488 |
st.setString(1, mdId); |
... | ... | |
414 | 492 |
return rs.getString("current_table"); |
415 | 493 |
} catch (SQLException e) { |
416 | 494 |
throw new MDStoreServiceException("Error on retrieve the current table from metadata for mdId " + mdId, e); |
495 |
} finally { |
|
496 |
if (connection != null) { |
|
497 |
try { |
|
498 |
connection.close(); |
|
499 |
} catch (SQLException e) { |
|
500 |
log.error("Error on close connection", e); |
|
501 |
throw new MDStoreServiceException(e); |
|
502 |
} |
|
503 |
} |
|
417 | 504 |
} |
418 | 505 |
|
419 | 506 |
} |
... | ... | |
429 | 516 |
* the MD store service exception |
430 | 517 |
*/ |
431 | 518 |
private void updateCurrentTable(final String transactionId, final String mdId) throws MDStoreServiceException { |
519 |
Connection connection = null; |
|
432 | 520 |
try { |
433 |
Connection connection = this.datasource.getConnection();
|
|
521 |
connection = this.datasource.getConnection(); |
|
434 | 522 |
String sql = String.format("update %s set current_table=? where mdid=?", METADATA_TABLE); |
435 | 523 |
PreparedStatement st = connection.prepareStatement(sql); |
436 | 524 |
st.setString(1, transactionId); |
... | ... | |
438 | 526 |
st.executeUpdate(); |
439 | 527 |
} catch (SQLException e) { |
440 | 528 |
throw new MDStoreServiceException("Error on update metadata table, for commit on metadata", e); |
529 |
} finally { |
|
530 |
if (connection != null) { |
|
531 |
try { |
|
532 |
connection.close(); |
|
533 |
} catch (SQLException e) { |
|
534 |
log.error("Error on close connection", e); |
|
535 |
throw new MDStoreServiceException(e); |
|
536 |
} |
|
537 |
} |
|
441 | 538 |
} |
442 | 539 |
|
443 | 540 |
} |
444 | 541 |
|
445 | 542 |
/* |
446 | 543 |
* (non-Javadoc) |
447 |
*
|
|
544 |
* |
|
448 | 545 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#readMdStore(java.lang.String) |
449 | 546 |
*/ |
450 | 547 |
@Override |
451 | 548 |
public String readMdStore(final String mdStoreId) throws MDStoreServiceException { |
549 |
Connection connection = null; |
|
452 | 550 |
try { |
453 |
Connection connection = this.datasource.getConnection();
|
|
551 |
connection = this.datasource.getConnection(); |
|
454 | 552 |
String currentTableName = getCurrentTable(mdStoreId, connection); |
455 | 553 |
String p = "WITH upsert AS (UPDATE %s SET dateofread=? WHERE id=? and mdstoreid=? RETURNING *) " |
456 | 554 |
+ " INSERT INTO %s (id, mdstoreid, dateofread) SELECT ?,?,? WHERE NOT EXISTS (SELECT * FROM upsert)"; |
... | ... | |
463 | 561 |
ps.setString(5, mdStoreId); |
464 | 562 |
ps.setTimestamp(6, new Timestamp(System.currentTimeMillis())); |
465 | 563 |
ps.executeUpdate(); |
564 |
return mdStoreId; |
|
466 | 565 |
|
467 | 566 |
} catch (SQLException e) { |
468 | 567 |
throw new MDStoreServiceException("Error on lock mdstore ", e); |
568 |
} finally { |
|
569 |
if (connection != null) { |
|
570 |
try { |
|
571 |
connection.close(); |
|
572 |
} catch (SQLException e) { |
|
573 |
log.error("Error on close connection", e); |
|
574 |
throw new MDStoreServiceException(e); |
|
575 |
} |
|
576 |
} |
|
469 | 577 |
} |
470 | 578 |
|
471 |
return null; |
|
472 | 579 |
} |
473 | 580 |
|
474 | 581 |
/* |
475 | 582 |
* (non-Javadoc) |
476 |
*
|
|
583 |
* |
|
477 | 584 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#getInfoForCurrentMdStore(java.lang.String) |
478 | 585 |
*/ |
479 | 586 |
@Override |
480 | 587 |
public MDStoreManagerInfo getInfoForCurrentMdStore(final String mdStoreId) throws MDStoreServiceException { |
588 |
Connection connection = null; |
|
481 | 589 |
try { |
590 |
connection = this.datasource.getConnection(); |
|
482 | 591 |
MDStoreManagerInfo info = new MDStoreManagerInfo(); |
483 | 592 |
String currenttable = getCurrentTable(mdStoreId, this.datasource.getConnection()); |
484 | 593 |
|
... | ... | |
486 | 595 |
List<MDStoreExpiredInfo> infoExpired = getJdbcTemplateObject().query(sql, new Object[] { mdStoreId }, new MDStoreReadLockInfo()); |
487 | 596 |
sql = String.format("select * from %s where mdstoreid=?", TRANSACTION_TABLE); |
488 | 597 |
List<MDStoreTransactionInfo> infotransactions = getJdbcTemplateObject().query(sql, new Object[] { mdStoreId }, new MDStoreTransactionMapper()); |
489 |
Connection connection = this.datasource.getConnection(); |
|
490 | 598 |
if (infotransactions != null) { |
491 | 599 |
for (MDStoreTransactionInfo i : infotransactions) { |
492 | 600 |
i.setSize(getSizeOfTable(i.getId(), connection)); |
... | ... | |
499 | 607 |
return info; |
500 | 608 |
} catch (SQLException e) { |
501 | 609 |
throw new MDStoreServiceException(e); |
610 |
} finally { |
|
611 |
if (connection != null) { |
|
612 |
try { |
|
613 |
connection.close(); |
|
614 |
} catch (SQLException e) { |
|
615 |
log.error("Error on close connection", e); |
|
616 |
throw new MDStoreServiceException(e); |
|
617 |
} |
|
618 |
} |
|
502 | 619 |
} |
503 | 620 |
} |
504 | 621 |
|
... | ... | |
527 | 644 |
|
528 | 645 |
/* |
529 | 646 |
* (non-Javadoc) |
530 |
*
|
|
647 |
* |
|
531 | 648 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#dropUsed(java.lang.String, java.lang.String) |
532 | 649 |
*/ |
533 | 650 |
@Override |
534 | 651 |
public Boolean dropUsed(final String mdId, final String idToDrop) throws MDStoreServiceException { |
652 |
Connection conn = null; |
|
535 | 653 |
try { |
536 |
Connection conn = this.datasource.getConnection();
|
|
654 |
conn = this.datasource.getConnection(); |
|
537 | 655 |
String sql = "delete from readlock where id=? and mdstoreid=?"; |
538 | 656 |
PreparedStatement statement = conn.prepareStatement(sql); |
539 | 657 |
log.info("deleting collection from readlock table:" + idToDrop); |
540 | 658 |
statement.setString(1, idToDrop); |
541 | 659 |
statement.setString(2, mdId); |
542 | 660 |
statement.executeUpdate(); |
543 |
dropCollection(idToDrop); |
|
661 |
String currentTable = getCurrentTable(mdId, conn); |
|
662 |
if (!currentTable.equals(idToDrop)) { |
|
663 |
dropCollection(idToDrop); |
|
664 |
} |
|
544 | 665 |
return true; |
545 | 666 |
} catch (SQLException e) { |
546 | 667 |
throw new MDStoreServiceException("Unable to delete a readlock table with id:" + idToDrop, e); |
668 |
} finally { |
|
669 |
if (conn != null) { |
|
670 |
try { |
|
671 |
conn.close(); |
|
672 |
} catch (SQLException e) { |
|
673 |
log.error("Error on close connection", e); |
|
674 |
throw new MDStoreServiceException(e); |
|
675 |
} |
|
676 |
} |
|
547 | 677 |
} |
548 | 678 |
|
549 | 679 |
} |
550 | 680 |
|
551 | 681 |
/* |
552 | 682 |
* (non-Javadoc) |
553 |
*
|
|
683 |
* |
|
554 | 684 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#dropTransaction(java.lang.String, java.lang.String) |
555 | 685 |
*/ |
556 | 686 |
@Override |
557 | 687 |
public Boolean dropTransaction(final String mdId, final String idToDrop) throws MDStoreServiceException { |
688 |
Connection conn = null; |
|
558 | 689 |
try { |
559 |
Connection conn = this.datasource.getConnection();
|
|
690 |
conn = this.datasource.getConnection(); |
|
560 | 691 |
String sql = "delete from transactions where id=? and mdstoreid=?"; |
561 | 692 |
PreparedStatement statement = conn.prepareStatement(sql); |
562 | 693 |
log.info("deleting collection from transactions table:" + idToDrop); |
... | ... | |
567 | 698 |
return true; |
568 | 699 |
} catch (SQLException e) { |
569 | 700 |
throw new MDStoreServiceException("Unable to delete a transaction table with id:" + idToDrop, e); |
701 |
} finally { |
|
702 |
if (conn != null) { |
|
703 |
try { |
|
704 |
conn.close(); |
|
705 |
} catch (SQLException e) { |
|
706 |
log.error("Error on close connection", e); |
|
707 |
throw new MDStoreServiceException(e); |
|
708 |
} |
|
709 |
} |
|
570 | 710 |
} |
571 | 711 |
} |
572 | 712 |
|
573 | 713 |
/* |
574 | 714 |
* (non-Javadoc) |
575 |
*
|
|
715 |
* |
|
576 | 716 |
* @see eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager#garbage() |
577 | 717 |
*/ |
578 | 718 |
@Override |
579 | 719 |
public void garbage() throws MDStoreServiceException { |
580 | 720 |
verifyConsistency(); |
581 | 721 |
String findOldStoresql = "select r.id from readlock r, metadata m where r.mdstoreid = m.mdid and r.id != current_table and extract (day from (now() - r.dateofread)) >=?"; |
722 |
Connection conn = null; |
|
582 | 723 |
try { |
724 |
conn = this.datasource.getConnection(); |
|
583 | 725 |
log.info("Start garbage collection of the unused mdstore"); |
584 |
Connection conn = this.datasource.getConnection(); |
|
585 | 726 |
PreparedStatement ps = conn.prepareStatement(findOldStoresql); |
586 | 727 |
ps.setInt(1, expiredDays); |
587 | 728 |
ResultSet rs = ps.executeQuery(); |
... | ... | |
602 | 743 |
|
603 | 744 |
} catch (SQLException e) { |
604 | 745 |
throw new MDStoreServiceException("Unable to start garbage of the mdstores", e); |
746 |
} finally { |
|
747 |
if (conn != null) { |
|
748 |
try { |
|
749 |
conn.close(); |
|
750 |
} catch (SQLException e) { |
|
751 |
log.error("Error on close connection", e); |
|
752 |
throw new MDStoreServiceException(e); |
|
753 |
} |
|
754 |
} |
|
605 | 755 |
} |
606 | 756 |
|
607 | 757 |
} |
modules/dnet-postgres-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/postgres/MDStoreDaoPostgresImpl.java | ||
---|---|---|
32 | 32 |
public class MDStoreDaoPostgresImpl implements MDStoreDao { |
33 | 33 |
|
34 | 34 |
/** The datasource. */ |
35 |
@Autowired
|
|
35 |
@javax.annotation.Resource(name = "mdstoreDataSource")
|
|
36 | 36 |
private DataSource datasource; |
37 | 37 |
|
38 | 38 |
/** The metadata table. */ |
... | ... | |
85 | 85 |
*/ |
86 | 86 |
@Override |
87 | 87 |
public MDStore getMDStore(final String mdId) throws MDStoreServiceException { |
88 |
Connection conn = null; |
|
88 | 89 |
try { |
89 |
Connection conn = datasource.getConnection();
|
|
90 |
conn = datasource.getConnection(); |
|
90 | 91 |
verifyConsistency(conn); |
91 | 92 |
String mdProfileID = transactionManager.getMDStoreForTransaction(mdId); |
92 |
String sql = String.format("select * from %s where %s = ?", METADATA_TABLE, MDIDColumnName); |
|
93 |
String sql = String.format("select * from %s where %s = ? or current_table= ?", METADATA_TABLE, MDIDColumnName); |
|
94 |
if (mdProfileID != null) { |
|
95 |
sql = String.format("select * from %s where %s = ?", METADATA_TABLE, MDIDColumnName); |
|
96 |
} |
|
93 | 97 |
PreparedStatement ps = conn.prepareStatement(sql); |
94 | 98 |
if (mdProfileID != null) { |
95 | 99 |
ps.setString(1, mdProfileID); |
96 | 100 |
} else { |
97 | 101 |
ps.setString(1, mdId); |
102 |
ps.setString(2, mdId); |
|
98 | 103 |
} |
99 | 104 |
ResultSet rs = ps.executeQuery(); |
100 | 105 |
if (!rs.next()) throw new MDStoreServiceException("Collection not found for MDStoreID :" + (mdProfileID != null ? mdProfileID : mdId)); |
... | ... | |
110 | 115 |
} catch (Exception e) { |
111 | 116 |
log.error("Error on get mdstore", e); |
112 | 117 |
throw new MDStoreServiceException(e); |
118 |
} finally { |
|
119 |
if (conn != null) { |
|
120 |
try { |
|
121 |
conn.close(); |
|
122 |
} catch (SQLException e) { |
|
123 |
log.error("Error on close connection", e); |
|
124 |
throw new MDStoreServiceException(e); |
|
125 |
} |
|
126 |
} |
|
113 | 127 |
} |
114 | 128 |
} |
115 | 129 |
|
... | ... | |
152 | 166 |
*/ |
153 | 167 |
@Override |
154 | 168 |
public List<String> listMDStores() throws MDStoreServiceException { |
169 |
Connection conn = null; |
|
155 | 170 |
try { |
156 |
Connection conn = datasource.getConnection();
|
|
171 |
conn = datasource.getConnection(); |
|
157 | 172 |
verifyConsistency(conn); |
158 | 173 |
String query = "select " + MDIDColumnName + " from metadata"; |
159 | 174 |
PreparedStatement st = conn.prepareStatement(query); |
... | ... | |
166 | 181 |
} catch (Exception e) { |
167 | 182 |
log.error("Error on listing mdstore", e); |
168 | 183 |
throw new MDStoreServiceException(e); |
184 |
} finally { |
|
185 |
if (conn != null) { |
|
186 |
try { |
|
187 |
conn.close(); |
|
188 |
} catch (SQLException e) { |
|
189 |
log.error("Error on close connection", e); |
|
190 |
throw new MDStoreServiceException(e); |
|
191 |
} |
|
192 |
} |
|
169 | 193 |
} |
170 | 194 |
} |
171 | 195 |
|
... | ... | |
176 | 200 |
*/ |
177 | 201 |
@Override |
178 | 202 |
public List<String> listMDStores(final String format, final String layout, final String interpretation) throws MDStoreServiceException { |
203 |
Connection conn = null; |
|
179 | 204 |
try { |
180 |
Connection conn = datasource.getConnection();
|
|
205 |
conn = datasource.getConnection(); |
|
181 | 206 |
verifyConsistency(conn); |
182 | 207 |
String query = "select " + MDIDColumnName + " from metadata where lower(format) = ? and lower(layout) = ? and lower(interpretation) = ?"; |
183 | 208 |
PreparedStatement st = conn.prepareStatement(query); |
... | ... | |
193 | 218 |
} catch (Exception e) { |
194 | 219 |
log.error("Error on listing mdstore", e); |
195 | 220 |
throw new MDStoreServiceException(e); |
221 |
} finally { |
|
222 |
if (conn != null) { |
|
223 |
try { |
|
224 |
conn.close(); |
|
225 |
} catch (SQLException e) { |
|
226 |
log.error("Error on close connection", e); |
|
227 |
throw new MDStoreServiceException(e); |
|
228 |
} |
|
229 |
} |
|
196 | 230 |
} |
197 | 231 |
} |
198 | 232 |
|
... | ... | |
204 | 238 |
*/ |
205 | 239 |
@Override |
206 | 240 |
public void createMDStore(final String mdId, final String format, final String interpretation, final String layout) throws MDStoreServiceException { |
241 |
|
|
242 |
Connection conn = null; |
|
207 | 243 |
try { |
208 |
Connection conn = datasource.getConnection();
|
|
244 |
conn = datasource.getConnection(); |
|
209 | 245 |
verifyConsistency(conn); |
210 |
String query = "insert into " + METADATA_TABLE + " values (?,?,?,?,?,?)"; |
|
246 |
String query = "insert into " + METADATA_TABLE + " values (?,?,?,?,?,?,?)";
|
|
211 | 247 |
PreparedStatement st = conn.prepareStatement(query); |
212 | 248 |
st.setString(1, mdId); |
213 | 249 |
st.setString(2, format); |
... | ... | |
221 | 257 |
|
222 | 258 |
st.setString(5, "mdstore_" + idName.replace("-", "_")); |
223 | 259 |
st.setInt(6, 0); |
260 |
st.setInt(7, 0); |
|
224 | 261 |
log.debug(st.toString()); |
225 | 262 |
st.executeUpdate(); |
226 | 263 |
final StringTemplate templateCollection = new StringTemplate(IOUtils.toString(getTemplateCollection().getInputStream())); |
... | ... | |
231 | 268 |
} catch (Exception e) { |
232 | 269 |
log.error("Error on inserting mdstore", e); |
233 | 270 |
throw new MDStoreServiceException(e); |
271 |
} finally { |
|
272 |
if (conn != null) { |
|
273 |
try { |
|
274 |
conn.close(); |
|
275 |
} catch (SQLException e) { |
|
276 |
log.error("Error on close connection", e); |
|
277 |
throw new MDStoreServiceException(e); |
|
278 |
} |
|
279 |
} |
|
234 | 280 |
} |
235 | 281 |
} |
236 | 282 |
|
... | ... | |
241 | 287 |
*/ |
242 | 288 |
@Override |
243 | 289 |
public void deleteMDStore(final String mdId) throws MDStoreServiceException { |
290 |
Connection conn = null; |
|
244 | 291 |
try { |
245 |
Connection conn = datasource.getConnection();
|
|
292 |
conn = datasource.getConnection(); |
|
246 | 293 |
verifyConsistency(conn); |
247 | 294 |
// RETREIVE THE CURRENT USED TABLE ON METADATA |
248 | 295 |
String sql = String.format("select * from %s where %s = ?", METADATA_TABLE, MDIDColumnName); |
... | ... | |
271 | 318 |
} catch (Exception e) { |
272 | 319 |
log.error("Error on deleting mdstore", e); |
273 | 320 |
throw new MDStoreServiceException(e); |
321 |
} finally { |
|
322 |
if (conn != null) { |
|
323 |
try { |
|
324 |
conn.close(); |
|
325 |
} catch (SQLException e) { |
|
326 |
log.error("Error on close connection", e); |
|
327 |
throw new MDStoreServiceException(e); |
|
328 |
} |
|
329 |
} |
|
274 | 330 |
} |
275 | 331 |
|
276 | 332 |
} |
modules/dnet-postgres-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/inspector/MDStorePostgresInspector.java | ||
---|---|---|
1 |
package eu.dnetlib.data.mdstore.modular.inspector; |
|
2 |
|
|
3 |
import static eu.dnetlib.miscutils.collections.MappedCollection.listMap; |
|
4 |
|
|
5 |
import java.io.StringReader; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
|
|
10 |
import javax.annotation.Resource; |
|
11 |
|
|
12 |
import org.apache.commons.logging.Log; |
|
13 |
import org.apache.commons.logging.LogFactory; |
|
14 |
import org.dom4j.Document; |
|
15 |
import org.dom4j.DocumentException; |
|
16 |
import org.dom4j.io.SAXReader; |
|
17 |
import org.springframework.stereotype.Controller; |
|
18 |
import org.springframework.ui.Model; |
|
19 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
20 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
21 |
import org.springframework.web.bind.annotation.RequestParam; |
|
22 |
|
|
23 |
import com.google.common.base.Function; |
|
24 |
import com.google.common.collect.Lists; |
|
25 |
|
|
26 |
import eu.dnetlib.data.mdstore.MDStoreServiceException; |
|
27 |
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder; |
|
28 |
import eu.dnetlib.data.mdstore.modular.connector.MDStore; |
|
29 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao; |
|
30 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo; |
|
31 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager; |
|
32 |
import eu.dnetlib.enabling.inspector.AbstractInspectorController; |
|
33 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
34 |
import eu.dnetlib.miscutils.collections.MappedCollection; |
|
35 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
36 |
import eu.dnetlib.miscutils.functional.xml.TryIndentXmlString; |
|
37 |
|
|
38 |
@Controller |
|
39 |
public class MDStorePostgresInspector extends AbstractInspectorController { |
|
40 |
|
|
41 |
private static final Log log = LogFactory.getLog(MDStorePostgresInspector.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
42 |
|
|
43 |
@Resource(name = "postgresMDStoreDao") |
|
44 |
private MDStoreDao mdstoreDao; |
|
45 |
|
|
46 |
@Resource(name = "postgresTransactionManager") |
|
47 |
private MDStoreTransactionManager transactionManager; |
|
48 |
|
|
49 |
@Resource |
|
50 |
private MDStoreFeeder feeder; |
|
51 |
|
|
52 |
class MDStoreDescription { |
|
53 |
|
|
54 |
private String id; |
|
55 |
|
|
56 |
private int size; |
|
57 |
|
|
58 |
public MDStoreDescription(final String id, final int size) { |
|
59 |
super(); |
|
60 |
this.id = id; |
|
61 |
this.size = size; |
|
62 |
} |
|
63 |
|
|
64 |
public String getFormat() { |
|
65 |
try { |
|
66 |
return mdstoreDao.getMDStore(id).getFormat(); |
|
67 |
} catch (MDStoreServiceException e) { |
|
68 |
return null; |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
public String getLayout() { |
|
73 |
try { |
|
74 |
return mdstoreDao.getMDStore(id).getLayout(); |
|
75 |
} catch (MDStoreServiceException e) { |
|
76 |
return null; |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
public String getInterpretation() { |
|
81 |
try { |
|
82 |
return mdstoreDao.getMDStore(id).getInterpretation(); |
|
83 |
} catch (MDStoreServiceException e) { |
|
84 |
return null; |
|
85 |
} |
|
86 |
} |
|
87 |
|
|
88 |
public boolean getIndexed() { |
|
89 |
|
|
90 |
return true; |
|
91 |
|
|
92 |
} |
|
93 |
|
|
94 |
public String getId() { |
|
95 |
return id; |
|
96 |
} |
|
97 |
|
|
98 |
public void setId(final String id) { |
|
99 |
this.id = id; |
|
100 |
} |
|
101 |
|
|
102 |
public int getSize() { |
|
103 |
return size; |
|
104 |
} |
|
105 |
|
|
106 |
public void setSize(final int size) { |
|
107 |
this.size = size; |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
@RequestMapping(value = "/inspector/mdstores.do") |
|
112 |
public void mdstores(final Model model) throws MDStoreServiceException { |
|
113 |
model.addAttribute("mdstores", MappedCollection.listMap(mdstoreDao.listMDStores(), new UnaryFunction<MDStoreDescription, String>() { |
|
114 |
|
|
115 |
@Override |
|
116 |
public MDStoreDescription evaluate(final String arg) { |
|
117 |
try { |
|
118 |
String currentid = transactionManager.getMDStoreCollection(arg); |
|
119 |
MDStore mdstore = mdstoreDao.getMDStore(currentid); |
|
120 |
return new MDStoreDescription(arg, mdstore.getSize()); |
|
121 |
} catch (MDStoreServiceException e) { |
|
122 |
return null; |
|
123 |
} |
|
124 |
|
|
125 |
} |
|
126 |
})); |
|
127 |
} |
|
128 |
|
|
129 |
@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.GET) |
|
130 |
public void mdstore(final Model model, |
|
131 |
@RequestParam("id") final String id, |
|
132 |
@RequestParam(value = "start", required = false) final Integer startParam, |
|
133 |
@RequestParam(value = "regex", required = false) final String regex) throws MDStoreServiceException { |
|
134 |
int pageSize = 10; |
|
135 |
int start = 0; |
|
136 |
|
|
137 |
if (startParam != null) { |
|
138 |
start = startParam; |
|
139 |
} |
|
140 |
|
|
141 |
String currentId = transactionManager.getMDStoreCollection(id); |
|
142 |
|
|
143 |
MDStore mdstore = mdstoreDao.getMDStore(currentId); |
|
144 |
|
|
145 |
ResultSetListener rs = mdstore.deliver(null, null, regex); |
|
146 |
Function<String, Map<String, String>> function = new Function<String, Map<String, String>>() { |
|
147 |
|
|
148 |
private SAXReader reader = new SAXReader(); |
|
149 |
|
|
150 |
@Override |
|
151 |
public Map<String, String> apply(final String input) { |
|
152 |
try { |
|
153 |
|
Also available in: Unified diff
implemented postgres mdstore, added some exception