如果我在grails中使用groovy sql类,它是否使用grails连接池? [英] If I use groovy sql class in grails, does it use the grails connection pooling?
问题描述
def db = [url:'jdbc:hsqldb:mem:testDB',用户:'sa',密码:'',驱动程序:'org.hsqldb.jdbcDriver']
def sql = Sql.newInstance(db.url,db.user,db.password,db.driver)
或者如果您有现有连接(可能来自连接池)或数据源使用其中一个构造函数:
def sql = new Sql(datasource)
现在您可以调用sql,例如创建一个表:
sql.execute'''
$ p $
create table PROJECT(
id integer not null,
name varchar(50),$ b $ url url varchar(100),
)
'''
解决方案如果您执行:
Sql.newInstance(...)
您将创建一个新连接,但您并未使用连接池。
如果您想使用连接池,您可以使用以下命令创建一个服务:
grails create-service org.foo.MyService
然后,在您的MyService.groovy文件中,您可以如下管理事务:
import javax.annotation.PostConstruct
class MyService {
def dataSource //注入数据源
static transactional = true //告诉groovy服务方法ds将是事务性的
$ b $ def doSomething(){
sql = new Sql(dataSource)
//其余的代码
}
}
有关更多详情,请参阅: http://grails.org/doc/2.0.x/guide/services.html
编辑:
为了管理多个数据源,您可以根据您的Grails版本执行以下操作之一。 / p>
如果您使用的Grails版本大于1.1.1(不是2.x),则可以使用以下插件:
http://grails.org/plugin/datasources
如果您使用的是Grails 2.x,您可以使用开箱即用的支持:
http:// grails .org / doc / 2.0.0.RC1 / guide / conf.html#multipleDatasources
From the examples below in the sql documentation. If I use either of these ways to create a sql instance in the middle of a grails service class, will it use the grails connection pooling? Will it participate in any transaction capabilities? Do I need to close the connection myself? Or will it automatically go back into the pool?
def db = [url:'jdbc:hsqldb:mem:testDB', user:'sa', password:'', driver:'org.hsqldb.jdbcDriver'] def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
or if you have an existing connection (perhaps from a connection pool) or a datasource use one of the constructors:
def sql = new Sql(datasource)
Now you can invoke sql, e.g. to create a table:
sql.execute ''' create table PROJECT ( id integer not null, name varchar(50), url varchar(100), ) '''
解决方案If you execute:
Sql.newInstance(...)
You will create a new connection and you aren't using the Connection Pool.
If you want to use the connection pool, you can create a Service with the following command:
grails create-service org.foo.MyService
Then, in your MyService.groovy file, you can manage transactions as follows:
import javax.annotation.PostConstruct class MyService { def dataSource // inject the datasource static transactional = true // tell groovy that the service methods will be transactional def doSomething() { sql = new Sql(dataSource) //rest of your code } }
For more details you can read: http://grails.org/doc/2.0.x/guide/services.html
EDIT:
For manage multiple datasources you can do one of the following based on your Grails version.
If you are using a Grails version greater than 1.1.1 (not 2.x) you can use the following plugin:
http://grails.org/plugin/datasources
If you are using Grails 2.x you can use the out of the box support:
http://grails.org/doc/2.0.0.RC1/guide/conf.html#multipleDatasources
这篇关于如果我在grails中使用groovy sql类,它是否使用grails连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!