reference url:
http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html
when webSphere application server startup, hibernate uses WebSphere datasource.
step by step:
1 coding statup constom service
import javax.management.Notification;
import javax.management.NotificationFilterSupport;
import javax.management.NotificationListener;
import javax.management.ObjectName;
public class WebsphereStartup implements CustomService,NotificationListener {
public void initialize(Properties arg0) throws Exception {
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(NotificationConstants.TYPE_J2EE_STATE_RUNNING);
ObjectName target = new ObjectName("WebSphere:*,type=Server");
AdminServiceFactory.getAdminService().addNotificationListenerExtended(
target,
this,
filter,
null);
}
public void shutdown() throws Exception {
// When was shutdown, this method is executed
}
public void handleNotification(Notification notification, Object handback) {
// write init code here
init();
}
}
2 modify hibernate config file hibernate.cfg.xml
<property name="hibernate.connection.datasource">data source jndi</property>
<property name="hibernate.connection.username">name</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.session_factory_name">HibernateSessionFactory</property>
<property name="connection.autocommit">false</property>
<property name="net.sf.hibernate.transaction.JTATransactionFactory">net.sf.hibernate.transaction.WebSphereTransactionManagerLookup</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
注意:如果是非事务性数据源 那么在hibernate.cfg.xml 必须设置 <property name="connection.autocommit">false</property>
为false
3 create datasource jndi on was
如果是非事务性数据源,在建立数据源时,也要设置为非事务性数据源。
4 config custom service bean on Websphere
was console >servers> application server > server1>manage>custom service >new
5 start Server