Ebean.createSqlUpdate("truncate "+table+" cascade").execute() not delete anything?

1,300 views
Skip to first unread message

Freewind

unread,
Mar 15, 2011, 11:24:12 AM3/15/11
to Ebean ORM
I want clear all data before testing. I'm using postgresql.

Before each test, I use this code to clear all tables:

for(String table: tables) {
Ebean.createSqlUpdate("truncate "+table+" cascade").execute()
}

But seems not delete anything .

I added one user to table `users`, then:

int oriC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
int delC = Ebean.createSqlUpdate("truncate users
cascade").execute()
int currentC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
println(table+": ori="+oriC+", delC="+delC+", currentC="+currentC)

And it prints:

users: ori=1, delC=0, currentC=1

Where is wrong?

edge

unread,
Mar 15, 2011, 11:50:05 AM3/15/11
to Ebean ORM
well I'd assume you are not committing the transaction? You'll need a
commit in there at the end as Ebean expects that you know what you are
doing when you are running raw SQL and it doesn't do an implicit
commit.

Freewind

unread,
Mar 15, 2011, 12:04:07 PM3/15/11
to Ebean ORM
@Edge, thank you, I commit the transaction in my code, but I didn't
paste to the post.

I just find the solution: NOT use `Ebean.createSqlUpdate`, but use
`raw jdbc`

val tran = Ebean.beginTransaction()
try {
val conn = tran.getConnection()
val sql = "truncate users cascade"
conn.createStatement().executeUpdate(sql)
Ebean.commitTransaction()
} finally {
Ebean.endTransaction();
}
Ebean.getServerCacheManager().clearAll()

Now, it's OK

Rob Bygrave

unread,
Mar 15, 2011, 8:52:29 PM3/15/11
to eb...@googlegroups.com

Please post your complete example code for when it did not work.

I want to look at that.

Thanks, Rob.

2011/3/16 Freewind <nowi...@gmail.com>

Rob Bygrave

unread,
Mar 16, 2011, 2:53:12 AM3/16/11
to eb...@googlegroups.com, Freewind

No, I could not reproduce this. It works fine producing ori=1, delC=0, currentC=0.
 (I believe that is your second test case that didn't reproduce by the way).


Some Notes:

1. >>

Ebean expects that you know what you are
doing when you are running raw SQL and it doesn't do an implicit
commit.
<<
This is not correct. Ebean will wrap a transaction around your raw SqlQuery and SqlUpdate's if required just like every other operation.

2. For most DB's truncate is generally not transactional  (it can't be rolledback) - but that is a moot point here.

3. At the jdbc level with Postgres 8.4 jdbc driver truncate is not returning the number of rows truncated - I get zero back for this. That might be expected behaviour given the way truncate is generally used (you would use delete if you wanted this to be transactional and known the correct number of rows that were blatted).




2011/3/16 Freewind <nowi...@gmail.com>
protected def clearAll() {
   Ebean.beginTransaction()
   val oriC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
   val delC = Ebean.createSqlUpdate("truncate users
cascade").execute()
   val currentC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
   Ebean.commitTransaction()
   Ebean.getServerCacheManager().clearAll()
> 2011/3/16 Freewind <nowind...@gmail.com>

Freewind

unread,
Mar 15, 2011, 10:31:57 PM3/15/11
to Ebean ORM
protected def clearAll() {
Ebean.beginTransaction()
val oriC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
val delC = Ebean.createSqlUpdate("truncate users
cascade").execute()
val currentC = Ebean.createSqlQuery("select count(*) as c from
users").findUnique().getInteger("c")
Ebean.commitTransaction()
Ebean.getServerCacheManager().clearAll()
println(table+": ori="+oriC+", delC="+delC+", currentC="+currentC)
}

On 3月16日, 上午8时52分, Rob Bygrave <robin.bygr...@gmail.com> wrote:
> Please post your complete example code for when it did not work.
>
> I want to look at that.
>
> Thanks, Rob.
>
> 2011/3/16 Freewind <nowind...@gmail.com>
Reply all
Reply to author
Forward
0 new messages