# Register database

Beego ORM requires explicit registration of database information before it can be freely used.

And of course, never forget the anonymous introduction of the driver:

import (
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/lib/pq"
	_ "github.com/mattn/go-sqlite3"
)
1
2
3
4
5

From three drivers imported above, you can introduce one according to your needs.

Example:

// args[0]        Alias of the database, used to switch the database in ORM
// args[1]        driverName
// args[2]        DSN
orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8")

// args[3](optional)  max number of idle connections
// args[4](optional)  max number of connections (go >= 1.2)
maxIdle := 30
maxConn := 30
orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", orm.MaxIdleConnections(maxIdle), orm.MaxOpenConnections(maxConn))
1
2
3
4
5
6
7
8
9
10

ORM requires a default database to be registered. And Beego's ORM does not manage connections itself, but relies directly on the driver.

# Configuration

# Max number of connections

There are two ways to set the maximum number of open connections. One way is to use the MaxOpenConnections option when registering the database:

orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", orm.MaxOpenConnections(100))
1

Second way is to modify maximum number of open connections after registration:

orm.SetMaxOpenConns("default", 30)
1

# Max number of idle connections

There are two ways to set the maximum number of idle connections. One way is to use the MaxIdleConnections option when registering the database:

orm.RegisterDataBase("default", "mysql", "root:root@/orm_test?charset=utf8", orm.MaxIdleConnections(20))
1

Second way is to modify maximum number of idle connections after registration:

orm.SetMaxIdleConns("default", 30)
1

# Time zone

ORM uses time.Local as default time zone, and you can modify it by:

// Set UTC Time
orm.DefaultTimeLoc = time.UTC
1
2

ORM will get the time zone used by the database while doing RegisterDataBase and then do the corresponding conversion when accessing the time.Time type to match the time system. That operation will ensure that the time will not be wrong.

Notice:

  • Given the design of SQLite3, accesses default to UTC time

  • When using the go-sql-driver driver, please pay attention to the configuration. From a certain version, the driver uses UTC time by default instead of local time, so please specify the time zone parameter or access it in UTC time:

    For example: root:root@/orm_test?charset=utf8&loc=Asia%2FShanghai

    For more details, please refer to loc (opens new window) / parseTime (opens new window).

# Driver

Most of the time, you only need to use the default ones for drivers that have:

	DRMySQL                      // mysql
	DRSqlite                     // sqlite
	DROracle                     // oracle
	DRPostgres                   // pgsql
	DRTiDB                       // TiDB
1
2
3
4
5

If you need to register a custom driver, you can use:

// args[0]   driverName
// args[1]   driver implementation
// mysql / sqlite3 / postgres / tidb were registered automatically
orm.RegisterDriver("mysql", yourDriver)
1
2
3
4