I am wondering if it was possible to achieve SolrJ/Solr Two Phase Commit. Any examples? Any best practices?
What I know: * Lucene offers Two Phase Commit via it's index writer (prepareCommit() followed by either commit() or rollback()). http://lucene.apache.org/core/4_2_0/core/org/apache/lucene/index/IndexWriter.html * I know Solr Optimistic Concurrency is available. http://yonik.com/solr/optimistic-concurrency/ <http://yonik.com/solr/optimistic-concurrency/> I want a transactional behavior that ensures that there is a full commit or full rollback of multiple documents. I do not want to be in a situation where I don't know if the beans have been written or not written to the Solr instance. * Code Snippet > try { > UpdateResponse updateResponse = server.add(Arrays.asList(docOne, > docTwo)); > successForAddingDocuments = (updateResponse.getStatus() == 0); > if (successForAddingDocuments) { > UpdateResponse updateResponseForCommit = server.commit(); > successForCommit = (updateResponseForCommit.getStatus() == 0); > } > } catch (Exception e) { > } finally { > if (!successForCommit) { > System.err.println("Rolling back transaction."); > try { > UpdateResponse updateResponseForRollback = > server.rollback(); > if (updateResponseForRollback.getStatus() == 0) { > successForRollback = true; > } else { > successForRollback = false; > System.err.println("Failed to rollback! Bad as > state is now > unknown!"); > } > } catch (Exception e) { > } > } > } * Full Test class > @Test > public void documentTransactionTest() { > > try { > // HttpSolrServer server = ... > server.deleteById(Arrays.asList("1", "2")); > server.commit(); > } catch (Exception e) { > e.printStackTrace(); > } > > SolrInputDocument docOne = new SolrInputDocument(); > { > docOne.addField("id", 1L); > docOne.addField("type_s", "MyTestDoc"); > docOne.addField("value_s", "docOne"); > docOne.addField("_version_", -1L); > } > > SolrInputDocument docTwo = new SolrInputDocument(); > { > docTwo.addField("id", 2L); > docTwo.addField("type_s", "MyTestDoc"); > docTwo.addField("value_s", "docTwo"); > docTwo.addField("_version_", -1L); > } > > boolean successForAddingDocuments = false; > boolean successForCommit = false; > boolean successForRollback = false; > > // throw new SolrServerException("Connection Broken"); > > try { > UpdateResponse updateResponse = server.add(Arrays.asList(docOne, > docTwo)); > successForAddingDocuments = (updateResponse.getStatus() == 0); > if (successForAddingDocuments) { > UpdateResponse updateResponseForCommit = > server.commit(); > successForCommit = (updateResponseForCommit.getStatus() > == 0); > } > } catch (Exception e) { > } finally { > if (!successForCommit) { > System.err.println("Rolling back transaction."); > try { > UpdateResponse updateResponseForRollback = > server.rollback(); > if (updateResponseForRollback.getStatus() == 0) > { > successForRollback = true; > } else { > successForRollback = false; > System.err.println("Failed to rollback! > Bad as state is now > unknown!"); > } > } catch (Exception e) { > } > } > } > > { > try { > QueryResponse response = server.query(new > SolrQuery("*:*").addFilterQuery("type_s:MyTestDoc")); > > if (successForCommit) { > Assert.assertEquals(2, > response.getResults().size()); > Assert.assertEquals("docOne", > response.getResults().get(0).get("value_s")); > Assert.assertEquals("docTwo", > response.getResults().get(1).get("value_s")); > } else if (successForRollback) { > Assert.assertEquals(0, > response.getResults().size()); > } else { > // UNKNOWN STATE > > if (response.getResults().size() == 0) { > // rollback must have been successful > } else if (response.getResults().size() == 2) { > // commit was successful > } else { > Assert.fail(); > } > } > } catch (Exception e) { > Assert.fail(); > } > } > > } http://lucene.apache.org/solr/4_2_0/solr-solrj/org/apache/solr/client/solrj/impl/HttpSolrServer.html -- View this message in context: http://lucene.472066.n3.nabble.com/SolrJ-Solr-Two-Phase-Commit-tp4060399.html Sent from the Solr - User mailing list archive at Nabble.com.