configuring hibernate for production and testing
In my web app,I am using hibernate and hsqldb for database operations..and use Ant for build management.I have a local.properties containing db.url,db.driver etc.They contain production db values.
In hibernate.cfg.xml ,I am again providing these db parameters as properties inside SessionFactory elements.
In Ant buildfile ,I am using local.properties as the properties file so that I can create a target for starting db.
Finally ,In my dao implementation ,I am using a HibernateUtil class to create session factory With these in place I am able to access myappdb .
My problem is that ,I need to use a different db for testing purposes. I have some junit test classes and some cactus tests which need to work on myapptestdb.Currently what I am doing ,is replace all db related parameters in local.properties and hibernate.cfg.xml with the testdb values.
ie, replace myappdb with myapptestdb etc.. I know,this is not the correct way to do this..Can someone suggest a better solution for this?
thanks in advance,
mark
The relevant portions of various configuration files in my project are given below.
local.properties
db.driver=org.hsqldb.jdbcDriver
db.server.class=org.hsqldb.server.Server
db.url=jdbc:hsqldb:hsql://localhost:9005/myappdb
db.username=SA
db.password=SA
hport=9005
dir.data=data
halias=myappdb
hjar=lib/hsqldb.jar
hfile=--database.0 data/myappdb
...
hibernate.cfg.xml
<session-factory>
<property name="connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="connection.url">
jdbc:hsqldb:hsql://localhost:9005/myappdb;create=true
</property>
<property name="connection.username">SA</property>
<property name="connection.password">SA</property>
...
</session-factory>
build.xml
<property file="local.properties" />
...
<target name="startdb" description="start hsqldb in server mode">
<java fork="true" classname="${db.server.class}" >
<classpath>
<path location="${dir.lib}/hsqldb.jar"/>
</classpath>
<arg value="-database.0"/>
<arg value="${dir.data}/${halias}"/>
<arg value="-dbname.0"/>
<arg value="${halias}"/>
<arg value="-port"/>
<arg value="${hport}"/>
</java>
</target>
MyDaoImpl.java
class MyDaoImpl implements MyDao {
...
@Override
public Object findById(Long id) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
Object object = null;
try {
object = (Object) session.get(persistentClass, id);
return object;
} finally {
session.close();
}
}
...
}
HibernateUtil.java
class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null){
try{
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
开发者_Python百科 }catch(HibernateException e){
e.printStackTrace();
throw e;
}
}
return sessionFactory;
}
}
Two suggestions:
1) Move to Maven. There you can define <profiles/>
which allow you to configure settings for different environments (for example, development, testing, integration testing, production). (I'm not saying you can't do this with Ant; but it's a lot more natural under Maven and it doesn't require a lot of settings). You can also use resource filtering. There is also a separation between production and test code, without you having to do much more than follow a convention.
2) In Ant, you could have two separate copies of the properties file (located in different folders or under different names) and choose one to copy under the name you need - avoiding the need for you to programatically change things. You can do this by passing in a property at build execution time.
@markjason72:- few ideas:- i) for ant, why do you think db information storing in local.properties is bad idea? I think it is right way. You can create different version of properties files like local_dev.properties, local_test1.properties, local_prod.properties. and have your build.xml file to use different depending upon environment.
ii) For hibernate, you can actually share the properties files configured above for ant and remove those properties from hibernate.cfg.xml file. You can refer to hibernate property configuration for more detail.
精彩评论