Issue
I currently have my Hibernate configuration file hibernate.cfg.xml
in my Spring web app classpath and use Maven resources plugin to set different values for my dev and production databases like this:
reources/hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"../hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">@hibernate.connection.driver_class@</property>
<property name="hibernate.connection.url">@hibernate.connection.url@</property>
<property name="hibernate.dialect">@hibernate.dialect@</property>
<property name="hibernate.default_schema">@hibernate.default_schema@</property>
<property name="hibernate.connection.username">@hibernate.connection.username@</property>
<property name="hibernate.connection.password">@hibernate.connection.password@</property>
<property name="hibernate.connection.requireSSL">false</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<!--<property name="hbm2ddl.auto">update</property>-->
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.max_statements">50</property>
</session-factory>
</hibernate-configuration>
pom.xml (example) :
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<hibernate.connection.driver_class>com.mysql.cj.jdbc.Driver</hibernate.connection.driver_class>
<hibernate.connection.url>jdbc:mysql://localhost:3306/dev_db</hibernate.connection.url>
<hibernate.dialect>org.hibernate.dialect.MySQL5Dialect</hibernate.dialect>
<hibernate.default_schema></hibernate.default_schema>
<hibernate.connection.username>dev_username</hibernate.connection.username>
<hibernate.connection.password>dev_password</hibernate.connection.password>
</properties>
</profile>
<profile>
<id>deploy</id>
<properties>
<hibernate.connection.driver_class>com.mysql.cj.jdbc.Driver</hibernate.connection.driver_class>
<hibernate.connection.url>jdbc:mysql://localhost:3306/deploy_db</hibernate.connection.url>
<hibernate.dialect>org.hibernate.dialect.MySQL5Dialect</hibernate.dialect>
<hibernate.default_schema></hibernate.default_schema>
<hibernate.connection.username>deploy_username</hibernate.connection.username>
<hibernate.connection.password>deploy_password</hibernate.connection.password>
</properties>
</profile>
</profiles>
Those settings are loaded by a LocalSessionFactoryBean
like this:
@Bean( name = "sessionFactory" )
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource( dataSource() );
localSessionFactoryBean.setConfigLocation( new ClassPathResource( "hibernate.cfg.xml" ) );
localSessionFactoryBean.setPackagesToScan( "org.example" );
return localSessionFactoryBean;
}
But now I need to ship software to a client who obviously won't want me to know their DB connection properties, so I want to move these properties out of my project to some local resource (like C:\MyEpicProgram\db.properties
) which will be set separately in each environment (mine local, client's test server, client's deploy server etc.) and just linked to my app via JVM arguments (like -DdbConfigPath=C:\MyEpicProgram\db.properties
)
So that I get something like that: db.properties:
hibernate.connection.driver_class com.mysql.cj.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/client_db
hibernate.dialect org.hibernate.dialect.MySQL5Dialect
hibernate.default_schema
hibernate.connection.username client_username
hibernate.connection.password client_password
And those will be automatically used on project startup in that environment. How can I achieve that? I've been trying to modify my session factory bean to manually set up each parameter but as of now failed to do that correctly.
Solution
So, I have solved my problem like this, taking inspiration from Flavalacious' suggestion:
@Configuration
public class DBConfig {
private final Logger logger = Logger.getLogger( this.getClass() );
private String dataSourceDriverClass;
private String dataSourceDialect;
private String dataSourceUrl;
private String dataSourceUsername;
private String dataSourcePassword;
public DBConfig() {
Properties jdbcProperties = new Properties();
if ( System.getProperty( "dbConfigPath" ) == null )
throw new IllegalStateException( "Property -DdbConfigPath must be provided in VM options." );
try ( InputStream fileStream = new FileInputStream( filePath ) ) {
// load properties from file
jdbcProperties.load( fileStream );
// setting properties from file to local variables for usage in various beans
dataSourceUrl = jdbcProperties.getProperty( "url" );
dataSourceUsername = jdbcProperties.getProperty( "username" );
dataSourcePassword = jdbcProperties.getProperty( "password" );
dataSourceDriverClass = jdbcProperties.getProperty( "driverClass" );
dataSourceDialect = jdbcProperties.getProperty( "dialect" );
} catch ( IOException e ) {
logger.error( "Couldn't load JDBC properties." );
}
}
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
// use loaded parameters to configure datasource
driverManagerDataSource.setDriverClass( dataSourceDriverClass );
driverManagerDataSource.setJdbcUrl( dataSourceUrl );
driverManagerDataSource.setUser( dataSourceUsername );
driverManagerDataSource.setPassword( dataSourcePassword );
return driverManagerDataSource;
}
@Bean( name = "sessionFactory" )
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource( dataSource() );
localSessionFactoryBean.setConfigLocation( new ClassPathResource( "hibernate.cfg.xml" ) ); // the following properties are not present in this XML
localSessionFactoryBean.setPackagesToScan( "org.example" );
// set our properties to be discoverable by SessionFactory
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.connection.driver_class", dataSourceDriverClass );
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.connection.url", dataSourceUrl );
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.connection.username", dataSourceUsername );
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.connection.password", dataSourcePassword );
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.dialect", dataSourceDialect );
if ( dataSourceDefaultSchema != null )
localSessionFactoryBean.getHibernateProperties().setProperty( "hibernate.default_schema", dataSourceDefaultSchema );
return localSessionFactoryBean;
}
}
Path to the db.properties
file must be provided in VM options like this: -DdbConfigPath=C:\path\to\db.properties
Answered By - Pavel Polyakoff