You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Play项目配置Play Slick与PostgreSQL?首次配置遇阻求助

Troubleshooting Your Play Framework Startup & Repeated Error Issue

Hey there! Let's tackle this Play Framework issue you're dealing with—hitting http://localhost:9000/ right after launching and getting immediate exceptions, plus those annoying repeated errors every 2 seconds? Total pain. Since you’ve got a DAO set up for the user table and are calling its add method in your controller, here are the most likely culprits and fixes to check off:

  • Database Connection Pool Misconfiguration
    Play uses HikariCP under the hood for database connections, and if your settings in application.conf are off (wrong URL, bad credentials, missing driver), it’ll keep retrying connections every few seconds—hence the repeated logs.

    • Double-check your config snippet:
      db.default.driver=com.mysql.cj.jdbc.Driver # Or your DB's specific driver
      db.default.url="jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC"
      db.default.username=your_db_user
      db.default.password=your_db_password
      
    • Make sure your database server is actually running, and the user account has permissions to access the user table.
  • DAO Dependency Injection Failures
    If your DAO isn’t properly injected into your controller, Play can’t instantiate it correctly—leading to a crash when your action tries to call add().

    • Ensure you’re using constructor injection with the @Inject annotation (never instantiate the DAO with new!):
      public class UserController extends Controller {
          private final UserDAO userDAO;
      
          @Inject
          public UserController(UserDAO userDAO) {
              this.userDAO = userDAO;
          }
      
          public Result addUser() {
              userDAO.add(new User("Jane Smith"));
              return ok("User added");
          }
      }
      
    • Also, confirm your DAO is recognized by Play’s DI container—Guice should pick up classes automatically, but if you have custom bindings, double-check those configurations.
  • Missing Database Schema or Disabled Evolutions
    If the user table doesn’t exist in your database, calling add() will throw a "table not found" error. And if Play’s evolutions are turned off, it won’t auto-create the table for you.

    • Enable evolutions in application.conf:
      play.evolutions.enabled=true
      play.evolutions.autoApply=true # Auto-runs migration scripts without manual confirmation
      
    • Create a migration script at conf/evolutions/default/1.sql to set up the user table:
      #!Ups
      CREATE TABLE user (
          id BIGINT AUTO_INCREMENT PRIMARY KEY,
          name VARCHAR(255) NOT NULL,
          email VARCHAR(255) UNIQUE
      );
      
      #!Downs
      DROP TABLE IF EXISTS user;
      
  • Blocking Database Calls Clogging the Thread Pool
    Play’s controller actions are designed to be asynchronous, and if your DAO’s add method is a blocking JDBC call (no CompletionStage wrapper), it can exhaust the server’s thread pool—causing repeated errors as Play tries to handle the request.

    • Wrap your DAO call in an async action:
      import java.util.concurrent.CompletableFuture;
      import java.util.concurrent.CompletionStage;
      import play.mvc.HttpExecutionContext;
      
      public class UserController extends Controller {
          private final UserDAO userDAO;
          private final HttpExecutionContext ec;
      
          @Inject
          public UserController(UserDAO userDAO, HttpExecutionContext ec) {
              this.userDAO = userDAO;
              this.ec = ec;
          }
      
          public CompletionStage<Result> addUser() {
              // Run the blocking DAO call in a dedicated thread pool
              return CompletableFuture.supplyAsync(() -> {
                  userDAO.add(new User("Jane Smith"));
                  return ok("User added");
              }, ec.current());
          }
      }
      
  • Dig Into the Exact Stack Trace
    You mentioned logs have repeated errors—pull the full stack trace from your logs. Common red flags include:

    • SQLException: Access denied for user 'xxx'@'localhost' → Bad credentials or missing database permissions
    • SQLException: Table 'your_db.user' doesn't exist → Missing table or incorrect database name
    • NullPointerException → DAO wasn’t injected (you probably tried to instantiate it manually)

If you can share the exact error message from your logs, we can narrow this down even faster—but these checks should cover 90% of the cases with your setup.

内容的提问来源于stack exchange,提问作者Mirko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 06:22:41