如何为Play项目配置Play Slick与PostgreSQL?首次配置遇阻求助
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 inapplication.confare 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
usertable.
- Double-check your config snippet:
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 calladd().- Ensure you’re using constructor injection with the
@Injectannotation (never instantiate the DAO withnew!):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.
- Ensure you’re using constructor injection with the
Missing Database Schema or Disabled Evolutions
If theusertable doesn’t exist in your database, callingadd()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.sqlto set up theusertable:#!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;
- Enable evolutions in
Blocking Database Calls Clogging the Thread Pool
Play’s controller actions are designed to be asynchronous, and if your DAO’saddmethod is a blocking JDBC call (noCompletionStagewrapper), 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()); } }
- Wrap your DAO call in an async action:
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 permissionsSQLException: Table 'your_db.user' doesn't exist→ Missing table or incorrect database nameNullPointerException→ 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

