Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't init value outside the transaction, but it must be loaded #1574

Closed
zxcqirara opened this issue Aug 20, 2022 · 3 comments
Closed

Can't init value outside the transaction, but it must be loaded #1574

zxcqirara opened this issue Aug 20, 2022 · 3 comments

Comments

@zxcqirara
Copy link

zxcqirara commented Aug 20, 2022

val result = transaction {
	ServerRow.findById(server.id)!!.load(ServerRow::owner)
}

println(result.owner.lokin)

ServerRow

class ServerRow(id: EntityID<UUID>) : UUIDEntity(id) {
	companion object : UUIDEntityClass<ServerRow>(ServersTable)

	var name by ServersTable.name
	var description by ServersTable.description
	var members by UserRow via MembersTable
	var owner by UserRow referencedOn ServersTable.owner // Here is error
	val groups by GroupRow referrersOn GroupsTable.server
	val roles by RoleRow referrersOn RolesTable.server
	var createdAt by ServersTable.createdAt
}

UserRow

class UserRow(id: EntityID<UUID>) : UUIDEntity(id) {
	companion object : UUIDEntityClass<UserRow>(UsersTable)

	var lokin by UsersTable.lokin
	var password by UsersTable.password
	var username by UsersTable.username
	var region by UsersTable.locale
	var registeredAt by UsersTable.registeredAt
	var accessLevel by UsersTable.accessLevel
	var isBanned by UsersTable.isBanned
}

Error:

java.lang.IllegalStateException: Can't init value outside the transaction
	at org.jetbrains.exposed.sql.transactions.TransactionStore.getValue(TransactionScope.kt:21)
	at org.jetbrains.exposed.dao.EntityLifecycleInterceptorKt.isExecutedWithinEntityLifecycle(EntityLifecycleInterceptor.kt:12)
	at org.jetbrains.exposed.dao.EntityLifecycleInterceptorKt.executeAsPartOfEntityLifecycle(EntityLifecycleInterceptor.kt:15)
	at org.jetbrains.exposed.dao.Entity.getValue(Entity.kt:96)
	at run.flake.entities.database.ServerRow.getOwner(Servers.kt:29)
	at run.flake.routes.user_api.ServersRouteKt$servers$1$2.invokeSuspend(ServersRoute.kt:72)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:138)
	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:112)
	at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:14)
	at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:62)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
	at kotlinx.coroutines.UndispatchedCoroutine.afterResume(CoroutineContext.kt:233)
	at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:102)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at io.netty.util.concurrent.AbstractEventExecutor.runTask$$$capture(AbstractEventExecutor.java:174)
	at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:167)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:503)
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda-1$lambda-0(NettyApplicationEngine.kt:260)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:833)
@zxcqirara
Copy link
Author

@Tapac, help, please

@szakerx
Copy link

szakerx commented Sep 21, 2022

Tried this?
Database.connect( url = connectionUrl, user = username, password = password, databaseConfig = DatabaseConfig { keepLoadedReferencesOutOfTransaction = true } )

@Tapac
Copy link
Contributor

Tapac commented Oct 2, 2022

https://github.com/JetBrains/Exposed/blob/master/docs/ChangeLog.md#0351 Please check keepLoadedReferencesOutOfTransaction part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants