【发布时间】:2018-11-20 00:02:25
【问题描述】:
我想存储账本数据并仅从一个节点查询 我已经检查了this。为了将数据存储在表中,我遵循了flow cookbook of Corda 中给出的创建事务构建器的常规协议,但在记录事务时很受打击。
是否可以使用 FinalityFlow 来记录交易或必须使用 Service Hub 中的 recordTransactions 功能记录交易?
记录账外数据的首选方式是什么?
提前致谢
【问题讨论】:
标签: corda
我想存储账本数据并仅从一个节点查询 我已经检查了this。为了将数据存储在表中,我遵循了flow cookbook of Corda 中给出的创建事务构建器的常规协议,但在记录事务时很受打击。
是否可以使用 FinalityFlow 来记录交易或必须使用 Service Hub 中的 recordTransactions 功能记录交易?
记录账外数据的首选方式是什么?
提前致谢
【问题讨论】:
标签: corda
您应该通过直接在流中写入节点数据库来记录账外数据。
Flow DB 示例here 是执行此操作的 CorDapp 示例。它创建了一个DatabaseService,它存在于节点上并读写节点的数据库:
@CordaService
open class DatabaseService(private val services: ServiceHub) : SingletonSerializeAsToken() {
companion object {
val log = loggerFor<DatabaseService>()
}
/**
* Executes a database update.
*
* @param query The query string with blanks for the parameters.
* @param params The parameters to fill the blanks in the query string.
*/
protected fun executeUpdate(query: String, params: Map<Int, Any>) {
val preparedStatement = prepareStatement(query, params)
try {
preparedStatement.executeUpdate()
} catch (e: SQLException) {
log.error(e.message)
throw e
} finally {
preparedStatement.close()
}
}
/**
* Executes a database query.
*
* @param query The query string with blanks for the parameters.
* @param params The parameters to fill the blanks in the query string.
* @param transformer A function for processing the query's ResultSet.
*
* @return The list of transformed query results.
*/
protected fun <T : Any> executeQuery(
query: String,
params: Map<Int, Any>,
transformer: (ResultSet) -> T
): List<T> {
val preparedStatement = prepareStatement(query, params)
val results = mutableListOf<T>()
return try {
val resultSet = preparedStatement.executeQuery()
while (resultSet.next()) {
results.add(transformer(resultSet))
}
results
} catch (e: SQLException) {
log.error(e.message)
throw e
} finally {
preparedStatement.close()
}
}
/**
* Creates a PreparedStatement - a precompiled SQL statement to be
* executed against the database.
*
* @param query The query string with blanks for the parameters.
* @param params The parameters to fill the blanks in the query string.
*
* @return The query string and params compiled into a PreparedStatement
*/
private fun prepareStatement(query: String, params: Map<Int, Any>): PreparedStatement {
val session = services.jdbcSession()
val preparedStatement = session.prepareStatement(query)
params.forEach { (key, value) ->
when (value) {
is String -> preparedStatement.setString(key, value)
is Int -> preparedStatement.setInt(key, value)
is Long -> preparedStatement.setLong(key, value)
else -> throw IllegalArgumentException("Unsupported type.")
}
}
return preparedStatement
}
}
【讨论】:
【讨论】: