Grails 2 multiple dynamic datasources in services

前端 未结 4 1002
情歌与酒
情歌与酒 2020-12-14 22:37

I am working on a Grails application where I must access several datasources. The datasources are defined in the default database (ie. they are stored there and I must make

4条回答
  •  离开以前
    2020-12-14 22:58

    I worked on similar project where the application has to retrieve list of datasources (connection strings) from the default database, and connect to each datasource and perform operations using quartz jobs.

    I implemented it like, connect to each datasource within the application (not from DataSorce.groovy) and write SQL rather tahn HQL.

    import groovy.sql.Sql
    
    class SqlService{
        Sql getDbConnection(String connectionString, String dbUser, String dbPassword){
            def sql = Sql.newInstance(connectionString, dbUser, dbPassword, "driver_class")
            return sql
        }
    }
    

    Get sql connection from the above code and execute SQL queries using sql.execute "SQL STATEMENT" and close the sql connection. Here is the Sql class documentation.

提交回复
热议问题