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:
dankito 2020-04-28 18:16:16 +02:00
parent f1f34a1559
commit e2b957e50e
1 changed files with 12 additions and 19 deletions

View File

@ -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,13 +28,18 @@ 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 {
// 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 val fields = FieldBuilder() protected val fields = FieldBuilder()
protected var documentsWriter: DocumentsWriter? = null
protected val countWriterUsages = AtomicInteger(0)
override fun saveOrUpdateAccountTransactions(bankAccount: BankAccount, transactions: List<AccountTransaction>) { override fun saveOrUpdateAccountTransactions(bankAccount: BankAccount, transactions: List<AccountTransaction>) {
val writer = getWriter() val writer = getWriter()
@ -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()
}
} }
} }