Specify a schema using r2dbc-mssql

936 views Asked by At

Is there any way to specify a default schema in a properties file using r2dbc-mssql?

The connection works fine with:

spring:
  r2dbc:
    url: 'r2dbc:mssql://zzzzz.database.windows.net:1433/dbname'
    username: 'xxxxxx'
    password: 'xxxxxx'

but i have to use a static schema:

@Table("schemaname.foo")
public class Foo {

  @Id
  private Long id;

I've found something similar in r2dbc-postgresql: https://github.com/pgjdbc/r2dbc-postgresql/issues/37

1

There are 1 answers

1
Hantsy On

In a Spring Boot application, I think you can execute a sql statement to switch schemas in the @PostConstruct method of a @Configuration class.

@Configuration
class DatabaseConfig{

  @Autowired
  ConnectionFactory connectionFactory;

  @PostConstruct
  void init(){
      Mono.from(connectionFactory.getConnection())
          .flatMap(c->c.createStatement("switch to your schema here, different database has different commands here").execute())
          .subscribe()
  }
}