0

NOTE: I know I can fix this using scripts or a database field, but I am curious about accessing the connection string.

I have two testing environments. Each has its own database, one for Chinese and one for English data. Otherwise the two databases are identical.

The only difference is the connection string in my beans.xml (ctest vs ctestzh).

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="org.postgresql.Driver" />
		<property name="jdbcUrl" value="jdbc:postgresql://localhost/ctest?useUnicode=true&#38;characterEncoding=utf8" />
		<property name="user" value="testuser" />
		<property name="password" value="xxxx" />
....
	</bean>

	<!-- Hibernate 
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="org.postgresql.Driver" />
		<property name="jdbcUrl" value="jdbc:postgresql://localhost/ctestzh?useUnicode=true&#38;characterEncoding=utf8" />
		<property name="user" value="testuser" />
		<property name="password" value="xxxx" />
.....
	</bean>-->

I use an xml configuration file that configures my application to either process English or Chinese data. If I forget to change the beans.xml to the appropriate data, however, I corrupt the database (i.e., put Chinese data in the English database)

Can I access the connection string in code so I can fail if I am connected to the wrong database? I have looked at SessionFactory, but saw nothing obvious.

1 Answer 1

1
public String getConnectionString(DataSource dataSource) {
  return dataSource.getConnection().getMetaData().getURL();
}

See DatabaseMetaData class for more info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.