Hopefully now really fixed app crashes when restoring Android app due to previous DocumentsWriter / IndexWriter instance is still not destroyed and still holds write lock
This commit is contained in:
parent
f1f34a1559
commit
e2b957e50e
|
@ -20,7 +20,6 @@ import net.dankito.utils.serialization.ISerializer
|
||||||
import net.dankito.utils.serialization.JacksonJsonSerializer
|
import net.dankito.utils.serialization.JacksonJsonSerializer
|
||||||
import org.apache.lucene.index.IndexableField
|
import org.apache.lucene.index.IndexableField
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
|
||||||
|
|
||||||
|
|
||||||
open class LuceneBankingPersistence(
|
open class LuceneBankingPersistence(
|
||||||
|
@ -29,12 +28,17 @@ open class LuceneBankingPersistence(
|
||||||
serializer: ISerializer = JacksonJsonSerializer()
|
serializer: ISerializer = JacksonJsonSerializer()
|
||||||
) : BankingPersistenceJson(File(databaseFolder, "accounts.json"), serializer), IBankingPersistence {
|
) : BankingPersistenceJson(File(databaseFolder, "accounts.json"), serializer), IBankingPersistence {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
protected val fields = FieldBuilder()
|
// i really hate this solution, but could find no other way to avoid app crashes when
|
||||||
|
// Android app gets restored as previous IndexWriter is not not destroyed yet and holds
|
||||||
|
// write lock and a new IndexWriter instance in DocumentsWriter gets instantiated
|
||||||
protected var documentsWriter: DocumentsWriter? = null
|
protected var documentsWriter: DocumentsWriter? = null
|
||||||
|
|
||||||
protected val countWriterUsages = AtomicInteger(0)
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected val fields = FieldBuilder()
|
||||||
|
|
||||||
|
|
||||||
override fun saveOrUpdateAccountTransactions(bankAccount: BankAccount, transactions: List<AccountTransaction>) {
|
override fun saveOrUpdateAccountTransactions(bankAccount: BankAccount, transactions: List<AccountTransaction>) {
|
||||||
|
@ -48,8 +52,6 @@ open class LuceneBankingPersistence(
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.flushChangesToDisk()
|
writer.flushChangesToDisk()
|
||||||
|
|
||||||
releaseWriter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun createFieldsForAccountTransaction(bankAccount: BankAccount, transaction: AccountTransaction): List<IndexableField?> {
|
protected open fun createFieldsForAccountTransaction(bankAccount: BankAccount, transaction: AccountTransaction): List<IndexableField?> {
|
||||||
|
@ -73,22 +75,13 @@ open class LuceneBankingPersistence(
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
protected open fun getWriter(): DocumentsWriter {
|
protected open fun getWriter(): DocumentsWriter {
|
||||||
countWriterUsages.incrementAndGet()
|
|
||||||
|
|
||||||
documentsWriter?.let { return it }
|
documentsWriter?.let { return it }
|
||||||
|
|
||||||
documentsWriter = DocumentsWriter(LuceneConfig.getAccountTransactionsIndexFolder(indexFolder))
|
val writer = DocumentsWriter(LuceneConfig.getAccountTransactionsIndexFolder(indexFolder))
|
||||||
|
|
||||||
return documentsWriter!!
|
documentsWriter = writer
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
return writer
|
||||||
protected open fun releaseWriter() {
|
|
||||||
val countUsages = countWriterUsages.decrementAndGet()
|
|
||||||
|
|
||||||
if (countUsages == 0) {
|
|
||||||
documentsWriter?.close()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue