5

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!

dieend
  • 2,231
  • 1
  • 24
  • 29
  • You have to do DB-related things within a DB session. From what you describe it looks like there isn't an active session (or you don't use it) at the moment when you are trying to save your object. In the case of EBean API, may be you are simply missing the connection to the underlying table, look at this example : http://www.avaje.org/ebean/getstarted_props.html – Ashalynd Oct 13 '13 at 12:25
  • Did you ever solve this? Having similar issues... oddly calling Ebean.update(obj) works (until I do something more complicated), but Ebean.save(obj) never does. Maddening. – chris Oct 19 '13 at 16:25
  • 1
    So I solved this on my end. Switched to JPA (seems ebean may be moved to external package soon) and got not only the connection closed error, but also a (prior) error due to an SQL error. Had I configured ebean to output errors to console (unsure how to do this currently), I'd have caught it - SQL error occurrs, and the connection gets closed as a result. Might want to check if that is whats happening for you as well. – chris Oct 22 '13 at 19:02

4 Answers4

2

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.

Veera
  • 32,532
  • 36
  • 98
  • 137
0

One thing you might try is upgrading the mysql connector. The one you're using is a bit old (was released April 2011)

"mysql" % "mysql-connector-java" % "5.1.26"

If that doesn't help, then I would try making sure the credentials + connection URL are correct.

Jason Pearson
  • 609
  • 4
  • 15
0

I had the same problem (on Mac OS X with MySQL), but I have not used String ID

 User {
     @Id
     public long id;
     public String fullName;    
 }

when I tried:

Ebean.save(user) 

I got:

[PersistenceException: java.sql.SQLException: Connection is closed!]

Use the auto-increment strategy and update the mysql connector did not help me.

The problem was an attempt to write unicode in field, because without Unicode everything worked. On closer inspection, it turned out that the MySQL database was created without utf8_general_ci, but add this property using the ALTER on existing database does not work.

Create a new database with the default collation utf8_general_ci, worked for me.

0

Sometimes it could be a 'not null' column skipped during insert. In my case i have added a new column of 'int' type with 'not null' and forgot to insert values for them. Hope it might help some one.

prit4fun
  • 450
  • 1
  • 7
  • 15