Giter Site home page Giter Site logo

Comments (4)

jroper avatar jroper commented on May 14, 2024

As a workaround, could you try wrapping your onStart code in something that sets the context classloader to application.classloader?

from playframework.

omj avatar omj commented on May 14, 2024

I have found the source of an exception. When I define

object SomeObject {
  lazy val db = Database.forDataSource(DB.getDataSource())
  // usage of 'db'
  // ...
}

and simply run application, everything is "fine" - database connection is retrieved from datasource and used until application shutdown. When I run functional tests: first test loads SomeObject, initializes session, does some work and exits. Second test loads SomeObject and reuses session which is expired (datasource is already closed).

Is it a bug or a feature of framework to reuse static objects?

This is why

If I replace this code with the method call of some instance of some class

helped.

Changing lazy val db = ... to def db = ... fixes the problem.
Is it fine to initialize slick's session this way on every request?

Also, I have additional tests that doesn't use Play's FakeApplication but communicate with database directly (via Database.forURL("jdbc:postgresql://localhost/test", ...)
I get SQLException: No suitable driver found for jdbc:postgresql://localhost/test (DriverManager.java:190) if at least one functional test was executed before. I can rewrite them to use "fakes" as a workaround but wondering why does it happen.

P.S. @jroper Please point out some tutorial/explanation about Play's context class loader (is your suggestion is still relevant regarding information above?).

from playframework.

jroper avatar jroper commented on May 14, 2024

The context classloader is a JVM thing, not a Play thing, it's the classloader attached to the current running thread, and I believe it's what JDBC uses to locate SQL drivers. Setting it looks something like this:

val old = Thread.current().getContextClassLoader()
try {
  Thread.current().setContextClassLoader(application.classloader())
  // Your code here
} finally {
  Thread.current().setContextClassLoader(old)
}

from playframework.

jroper avatar jroper commented on May 14, 2024

Caching a database session statically is not something that should ever be done. You should update it to def.

from playframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.