Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL PersistenceException, "Connection is closed" in Play! 2.2

I'm trying out Play framework and using 2.2.0 version and Java version.

I want to use MySQL as the database, and I've followed the instruction at the site. I am able to get evolution script running, but I can't save anything to the database. The table created in the database. I got [PersistenceException: java.sql.SQLException: Connection is closed!] when trying to call Ebean.save() method, which is in my controller.

What I've done is:

  1. Set up build.sbt and added the plugin

    libraryDependencies ++= Seq(
        javaJdbc,
        javaEbean,
        cache,
        "mysql" % "mysql-connector-java" % "5.1.18"
    )
    
  2. Set up conf/application.conf

    db.default.driver=com.mysql.jdbc.Driver
    db.default.url="jdbc:mysql://localhost:3306/TLC?characterEncoding=UTF-8"
    db.default.user=root
    db.default.pass=secret
    
  3. Ebean server

    ebean.default="models.*"
    
  4. Created controller class package controllers;

    import com.avaje.ebean.Ebean;
    import play.*;
    import play.data.Form;
    import play.mvc.*;
    import models.Bar;
    import views.html.*;
    
    public class Application extends Controller {
    
        public static Result index() {
            return ok(index.render("Your new application is ready."));
        }
    
        public static Result addBar() {
            Bar bar = Form.form(Bar.class).bindFromRequest().get();
            bar.save();
            return redirect(routes.Application.index());
        }
    }
    
  5. Created a form

    <form action="@routes.Application.addBar()" method="post">
        <input name="name"/>
        <input type="submit"/>
    </form>
    
  6. Added the route

    POST    /bars                       controllers.Application.addBar()
    
  7. The model itself of course. package models;

    import play.db.ebean.Model;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Bar extends Model {
        @Id
        public String id;
        public String name;
    
    }
    
  8. And of course creating the database itself in mysql.

What did I miss? I've been on this like 4 hours and still have no idea what's wrong. If I used h2 in memory database, it works just fine. Please help me.

Thanks!

like image 631
dieend Avatar asked Nov 01 '22 14:11

dieend


1 Answers

I had similar issue and I found a solution.

The culprit is id property of your Bar entity. You have defined the id as String and you are not setting it while saving the Bar. Incase of Long, the id value is getting generated automatically, but for String you need to set it explicitly.

so,

Bar bar = Form.form(Bar.class).bindFromRequest().get();
bar.id = UUID.randomUUID().toString();
bar.save();

Should solve this issue. Hope this helps.

like image 60
Veera Avatar answered Nov 04 '22 17:11

Veera