{"record":{"id":"84a395e998188ad4","repo":"theonedev/onedev","slug":"server-not-ready","errorCode":null,"errorMessage":"Server not ready","messagePattern":"Server not ready","errorType":"http","errorClass":"ServerNotReadyException","httpStatus":503,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/persistence/DefaultSessionService.java","lineNumber":48,"sourceCode":"\t@Inject\n\tprivate SessionFactoryService sessionFactoryService;\n\t\n\tprivate final ThreadLocal<ObjectReference<Session>> sessionReferenceHolder = new ThreadLocal<ObjectReference<Session>>() {\n\n\t\t@Override\n\t\tprotected ObjectReference<Session> initialValue() {\n\t\t\treturn new ObjectReference<Session>() {\n\n\t\t\t\t@Override\n\t\t\t\tprotected Session openObject() {\n\t\t\t\t\tSessionFactory sessionFactory = sessionFactoryService.getSessionFactory();\n\t\t\t\t\tif (sessionFactory != null) {\n\t\t\t\t\t\tSession session = sessionFactory.openSession();\n\t\t\t\t\t\t// Session is supposed to be able to write only in transactional methods\n\t\t\t\t\t\tsession.setHibernateFlushMode(FlushMode.MANUAL);\n\t\t\t\t\t\treturn session;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new ServerNotReadyException();\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t@Override\n\t\t\t\tprotected void closeObject(Session session) {\n\t\t\t\t\tsession.close();\n\t\t\t\t}\n\t\t\t\t\n\t\t\t};\n\t\t}\n\t\t\n\t};\n\n\t@Override\n\tpublic void openSession() {\n\t\tsessionReferenceHolder.get().open();\n\t}\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/persistence/DefaultSessionService.java#L30-L66","documentation":"DefaultSessionService opens a thread-local Hibernate Session lazily via ObjectReference.openObject(). If SessionFactoryService has not yet built the SessionFactory (database not initialized/configured), a ServerNotReadyException with message \"Server not ready\" is thrown instead of returning a session. It signals that the caller attempted DB access during startup or before/while the server is shutting down.","triggerScenarios":"Calling sessionService.openSession(), getSession(), or running code inside sessionService.call()/run() before the SessionFactory exists — i.e. during OneDev bootstrap before DB upgrade/initialization completes, or after it was torn down.","commonSituations":"Plugin or extension code touching the database in an early startup/lifecycle phase; background threads started before DB init; accessing sessionService during server shutdown; REST/web requests arriving while server is still initializing.","solutions":["Wait until server startup completes (DB initialized) before doing DB work; defer work to an appropriate post-startup hook/listener","Wrap early-phase DB access with sessionService.call() which no-ops gracefully when the SessionFactory is null","Check sessionFactoryService.getSessionFactory() != null before calling getSession()/openSession()","If this appears on every request after startup, check server logs for a DB initialization failure that left the server not ready"],"exampleFix":"// before\nvar session = sessionService.getSession();\n// after\nif (sessionFactoryService.getSessionFactory() != null) {\n    var session = sessionService.getSession();\n} else {\n    logger.info(\"Server not ready yet; deferring DB work\");\n}","handlingStrategy":"try-catch","validationCode":"if (sessionFactoryService.getSessionFactory() == null) {\n    throw new IllegalStateException(\"Server not ready; defer DB access\");\n}","typeGuard":"boolean serverReady() { return sessionFactoryService.getSessionFactory() != null; }","tryCatchPattern":"try {\n    sessionService.run(() -> dao.doWork());\n} catch (ServerNotReadyException e) {\n    logger.info(\"Server not ready; retrying later\", e);\n    scheduleRetry();\n}","preventionTips":["Do DB work only from post-startup hooks, not constructors/init phases","Prefer sessionService.call()/run() which handles the not-ready case","Check sessionFactoryService.getSessionFactory() before direct session access","Watch startup logs for DB init failures"],"tags":["startup","hibernate","lifecycle"],"backgroundTag":"authentication-required","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}