mirror of
https://github.com/ACINQ/eclair.git
synced 2025-02-22 22:25:26 +01:00
Add exclusive locks to channels sqlite db (#781)
* added an exclusive write lock to channels sqlite db * added close() method in db traits
This commit is contained in:
parent
8887ac2043
commit
9da330478a
14 changed files with 40 additions and 1 deletions
|
@ -136,6 +136,7 @@ object NodeParams {
|
|||
chaindir.mkdir()
|
||||
|
||||
val sqlite = DriverManager.getConnection(s"jdbc:sqlite:${new File(chaindir, "eclair.sqlite")}")
|
||||
SqliteUtils.obtainExclusiveLock(sqlite) // there should only be one process writing to this file
|
||||
val channelsDb = new SqliteChannelsDb(sqlite)
|
||||
val peersDb = new SqlitePeersDb(sqlite)
|
||||
val pendingRelayDb = new SqlitePendingRelayDb(sqlite)
|
||||
|
|
|
@ -41,6 +41,8 @@ trait AuditDb {
|
|||
|
||||
def stats: Seq[Stats]
|
||||
|
||||
def close: Unit
|
||||
|
||||
}
|
||||
|
||||
case class NetworkFee(remoteNodeId: PublicKey, channelId: BinaryData, txId: BinaryData, feeSat: Long, txType: String, timestamp: Long)
|
||||
|
|
|
@ -31,4 +31,6 @@ trait ChannelsDb {
|
|||
|
||||
def listHtlcInfos(channelId: BinaryData, commitmentNumber: Long): Seq[(BinaryData, Long)]
|
||||
|
||||
def close(): Unit
|
||||
|
||||
}
|
||||
|
|
|
@ -55,4 +55,6 @@ trait NetworkDb {
|
|||
|
||||
def isPruned(shortChannelId: ShortChannelId): Boolean
|
||||
|
||||
def close(): Unit
|
||||
|
||||
}
|
||||
|
|
|
@ -41,6 +41,8 @@ trait PaymentsDb {
|
|||
|
||||
def listPayments(): Seq[Payment]
|
||||
|
||||
def close: Unit
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -28,4 +28,6 @@ trait PeersDb {
|
|||
|
||||
def listPeers(): Map[PublicKey, InetSocketAddress]
|
||||
|
||||
def close(): Unit
|
||||
|
||||
}
|
||||
|
|
|
@ -39,4 +39,6 @@ trait PendingRelayDb {
|
|||
|
||||
def listPendingRelay(channelId: BinaryData): Seq[Command]
|
||||
|
||||
def close(): Unit
|
||||
|
||||
}
|
||||
|
|
|
@ -201,4 +201,6 @@ class SqliteAuditDb(sqlite: Connection) extends AuditDb {
|
|||
}
|
||||
q
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close()
|
||||
}
|
||||
|
|
|
@ -101,4 +101,6 @@ class SqliteChannelsDb(sqlite: Connection) extends ChannelsDb {
|
|||
q
|
||||
}
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close
|
||||
}
|
||||
|
|
|
@ -149,4 +149,6 @@ class SqliteNetworkDb(sqlite: Connection) extends NetworkDb {
|
|||
rs.next()
|
||||
}
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close
|
||||
}
|
||||
|
|
|
@ -78,4 +78,5 @@ class SqlitePaymentsDb(sqlite: Connection) extends PaymentsDb with Logging {
|
|||
}
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close()
|
||||
}
|
||||
|
|
|
@ -75,4 +75,6 @@ class SqlitePeersDb(sqlite: Connection) extends PeersDb {
|
|||
m
|
||||
}
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close()
|
||||
}
|
||||
|
|
|
@ -60,4 +60,5 @@ class SqlitePendingRelayDb(sqlite: Connection) extends PendingRelayDb {
|
|||
}
|
||||
}
|
||||
|
||||
override def close(): Unit = sqlite.close()
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
package fr.acinq.eclair.db.sqlite
|
||||
|
||||
import java.sql.{ResultSet, Statement}
|
||||
import java.sql.{Connection, ResultSet, Statement}
|
||||
|
||||
import scodec.Codec
|
||||
import scodec.bits.BitVector
|
||||
|
@ -74,4 +74,20 @@ object SqliteUtils {
|
|||
}
|
||||
q
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain an exclusive lock on a sqlite database. This is useful when we want to make sure that only one process
|
||||
* accesses the database file (see https://www.sqlite.org/pragma.html).
|
||||
*
|
||||
* The lock will be kept until the database is closed, or if the locking mode is explicitely reset.
|
||||
*
|
||||
* @param sqlite
|
||||
*/
|
||||
def obtainExclusiveLock(sqlite: Connection){
|
||||
val statement = sqlite.createStatement()
|
||||
statement.execute("PRAGMA locking_mode = EXCLUSIVE")
|
||||
// we have to make a write to actually obtain the lock
|
||||
statement.executeUpdate("CREATE TABLE IF NOT EXISTS dummy_table_for_locking (a INTEGER NOT NULL)")
|
||||
statement.executeUpdate("INSERT INTO dummy_table_for_locking VALUES (42)")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue