Groovy gotcha: Passing zero arguments to a method that expects one
September 2nd, 2015
Groovy will compile just fine in certain scenarios where no arguments are passed to a method that expects one.
We had been working on this Grails 2 app for a few weeks and we were finally ready to put it on a test server (instead of running it like run-app
locally). More importantly we were ready to point it away from an H2 database and to SQLServer. But then we ran into problems…
SQLServer doesn’t have a boolean type, but instead will use int
. And we were using CLOBS and Hibernate was mapping them to text
, which is deprecated in SQLServer in favor of nvarchar(max)
. And a lot of other little things. The CLOB was important to us because we were using that a lot in this app. And there was a bit difference on how H2 handled it as opposed to how SQLServer did.
After googling around the solution came to be to write your own Hibernate Dialiact. That sounded daunting but it actually wasn’t. This is what we came up with:
import java.sql.Types
public class MySqlServerDialect extends org.hibernate.dialect.SQLServerDialect {
public MySqlServerDialect() {
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.BIT, "bit");
registerColumnType(Types.CHAR, "nchar(1)");
registerColumnType(Types.VARCHAR, 4000, "nvarchar(\$l)");
registerColumnType(Types.VARCHAR, "nvarchar(max)");
registerColumnType(Types.VARBINARY, 4000, "varbinary(\$1)");
registerColumnType(Types.VARBINARY, "varbinary(max)");
registerColumnType(Types.BLOB, "varbinary(max)");
registerColumnType(Types.CLOB, "nvarchar(max)");
}
}
And then you have to tell Grails about it. In the DataSource.groovy
add this to the SQLServer entries:
dialect = com.foo.MySqlServerDialect
And that should be it.
Groovy will compile just fine in certain scenarios where no arguments are passed to a method that expects one.
Getting Grails Database Connections to Reconnect
Details a method to share Grails object marshallers across XML, JSON and HAL flavors of web services.
Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.