问题
How to handle transaction management in Solr using Solrj? There is not much documentation related to this on the net. But I would appreciate if someone can provide any links or information related to transaction management using SolrJ.
回答1:
You would have to programatically deal with the transactions in SolrJ. When dealing with multiple writes.
- Use the SolrServer api add method to add the SolrInputDoucments to the server.
- When all the SolrInputDocuments are added, call the commit method from the SolrServer api to commit all the changes.
- In case of an exception from Solr, if you want to rollback the writes to the Solr, Call the SolServer api rollback method.
- If you want to rollback the writes to the database as well, just throw back a runtime-exception from the catch block.
This is how I dealt with the transaction management. If anyone has better answers, please feel free to improve the answer.
回答2:
The thing you have to keep in mind with Solr and transactions is that there is no isolation. Solr does not support transactions the way that most of us database developers are used to.
Commit makes all pending changes by all clients visible to new queries. Likewise, rollback rolls back all pending changes by all clients. There is zero consideration of which client sent the commit/rollback command.
For this reason, error handling shouldn't automatically result in a rollback. Because the impact may be much wider than just the data in error. And the cleanup may be much more difficult as a result.
The guidance from Solr documentation is to use auto-commit. This is especially true when performing bulk operations. If you are indexing in bulk, perhaps with multiple, parallel clients, then it's better to auto-commit every so often (or every so many documents). This causes fewer new index segments to be created and a less fragmented index overall as a result.
The details will depend on the mix of query and indexing operations happening on your Solr instance (and your replication approach).
There is a good Lucidworks article here: Understanding Transaction Logs, Soft Commit and Commit In SolrCloud
来源:https://stackoverflow.com/questions/10805117/solr-transaction-management-using-solrj