Java应用程序(非Web应用程序) + Hibernate + JTA 问题?

08-07-02 myjdon

你们好!

本人是一个Java初级学者,向你们请教一个问题:

Java应用程序(非web程序,即Java程序不是运行在Tomcat/Jboss等服务器上的)+ Hibernate + JTA实现跨数据库操作的问题,Hibernate配置如下:

文件a.hibernate.cfg.xml配置如下:

<hibernate-configuration>

<session-factory >

<property name="connection.url">jdbc:mysql://localhost:3306/database01</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="myeclipse.connection.profile">native mysql</property>

<property name="connection.username">root</property>

<property name="connection.password">root</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="show_sql">false</property>

<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<property name="jta.UserTransaction">java:comp/UserTransaction</property>

<mapping resource="Bbean.hbm.xml" />

</session-factory>

</hibernate-configuration>

文件b.hibernate.cfg.xml配置如下:

<hibernate-configuration>

<session-factory >

<property name="connection.url">jdbc:mysql://localhost:3306/database02</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="myeclipse.connection.profile">native mysql</property>

<property name="connection.username">root</property>

<property name="connection.password">root</property>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="show_sql">false</property>

<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<property name="jta.UserTransaction">java:comp/UserTransaction</property>

<mapping resource="Abean.hbm.xml" />

</session-factory>

</hibernate-configuration>

现在遇到的问题是:

<1>.上述配置是否可行? 若配置不正确,那么正确配置方式是?

<2>.用如上配置,业务代码如下:

Session abeanSession = AbeanSessionFactory.getSession();

Session bbeanSession = BbeanSessionFactory.getSession();

UserTransaction userTransaction = null;

try {

userTransaction = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");

userTransaction.begin();

//save Abean

Abean abean = new Artifact();

abean.setName("my_test");

abean.setSex(0);

abean = abeanDAO.save(abean, abeanSession);

//save Bbean

Bbean bbean = new Bbean();

control.setAbeand(abean.getArtifactID());

control.setState("Active");

bbeanDAO.save(bbean, bbeanSession);

userTransaction.commit();

} catch (Exception e) {

logger.error(e);

...

}

抛出异常是:

WARN - Could not bind factory to JNDI

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)

at javax.naming.InitialContext.getNameParser(Unknown Source)

at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)

at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)

at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)

at com.sears.tec.databaseaccess.factory.CatalogcentralSessionFactory.<clinit>(CatalogcentralSessionFactory.java:66)

at com.sears.tec.databaseaccess.dao.ArtifactAndControlDAOTest.setUp(ArtifactAndControlDAOTest.java:42)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

该怎么解决,多谢你不吝指教!!

[该贴被myjdon于2008-07-02 16:56修改过]

[该贴被myjdon于2008-07-02 16:56修改过]

[该贴被myjdon于2008-07-02 17:10修改过]

banq
2008-07-23 22:12

>Could not bind factory to JNDI

JNDI问题