Create Connections Using Suffixes

There are cases when you have to manage large database clusters where you have primaries and standby databases. I used to manage a cluster where the primary database host name was like pos1.db, pos2.db, ... and standbys were called pos1.sbydb, pos2.sbydb...

For this cases I have created the database connections like below.

groovy
import com.ecircle.dsql.connection.*

for ( int i=1;i <130; i++){
  ConnectionFactory.createConnector( "pos${i}", "postgres", "user=postgres host=pos${i}.db db=sakila${i}" )
}

ConnectionFactory.addSuffix( new Suffix("sby"){
  public Connector create( Connector con ){
   String host = con.getHost();
   if ( host.endsWith(".db") ) return duplicate( con, "host=" + host.replaceFirst(/\.db/, /.sbydb/))
   return null
  }
})
The Suffix class receive a connection. If the connection is matching the name .db, it duplicates the Connector by creating a new parameters string, where .db is replaced with .sbydb.

Navigation