JOTM事务管理时报JNDI错误问题,实在没辙了,熟悉JOTM的麻烦看下

 

主函数代码:

 

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		IUserServiceDS userService = (IUserServiceDS) ctx.getBean("userServiceDS");
		
		User1 user1 = new User1();
		user1.setName("user1");
		
		User2 user2 = new User2();
		user2.setName("user2");
		
		userService.insertBothUser(user1, user2);

 

DS相关代码:

 

	private User1DAO dao1;
	private User2DAO dao2;
	
	public void insertBothUser(User1 user1, User2 user2) throws Exception {
		this.insertUser1(user1);
		if(user2.getName().equalsIgnoreCase("user2")){
			throw new Exception();
		}
		else {
			this.insertUser2(user2);
		}
		
	}

	public void insertUser1(User1 user1) {
		dao1.save(user1);
	}

	public void insertUser2(User2 user2) {
		dao2.save(user2);
	}

 

applicationContext.xml内容:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- JOTM配置 开始 -->
	<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
	<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>

	<bean id="ds1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://10.129.68.170:3306/jotm1" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="sessFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="ds1" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/xa/model/User1.hbm.xml</value>
			</list>
		</property>
		<property name="jtaTransactionManager">
			<ref bean="jotm" />
		</property>
	</bean>

	<bean id="ds2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://10.129.68.170:3306/jotm2" />
			</bean>
		</property>
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>
	<bean id="sessFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="ds2" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/xa/model/User2.hbm.xml</value>
			</list>
		</property>
		<property name="jtaTransactionManager">
			<ref bean="jotm" />
		</property>
	</bean>
<!-- JOTM配置 结束 -->

	<bean id="User1DAO" class="com.xa.dao.User1DAO">
		<property name="sessionFactory">
			<ref bean="sessFactory1" />
		</property>
	</bean>
	<bean id="User2DAO" class="com.xa.dao.User2DAO">
		<property name="sessionFactory">
			<ref bean="sessFactory2" />
		</property>
	</bean>
	<bean id="userServiceDS" class="com.xa.service.UserServiceDS">
		<property name="dao1" ref="User1DAO" />
		<property name="dao2" ref="User2DAO" />
	</bean>

	<!-- 事务拦截 开始 -->
	<bean id="beanNameProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>*DS</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>
	<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="txManager" />
		<property name="transactionAttributeSource" ref="txAttributeSource" />
	</bean>
	<bean id="txAttributeSource"
		class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
		<property name="properties">
			<props>
				<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
			</props>
		</property>
	</bean>
	<!-- 事务拦截 结束 -->
</beans>

 

 carol.properties内容:

 

# do not use CAROL JNDI wrapper      
carol.start.jndi=false      
      
# do not start a name server 
carol.start.ns=false      
      
# Naming Factory   
carol.jndi.java.naming.factory.url.pkgs=org.apache.naming  

 运行时报的异常为:

 

org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,656 -- INFO -- RDBMS: MySQL, version: 5.1.34-community
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,656 -- INFO -- JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} )
org.hibernate.dialect.Dialect 2009-11-24 11:24:27,656 -- INFO -- Using dialect: org.hibernate.dialect.MySQLDialect
org.hibernate.transaction.TransactionFactoryFactory 2009-11-24 11:24:27,656 -- INFO -- Transaction strategy: org.hibernate.transaction.JTATransactionFactory
org.hibernate.util.NamingHelper 2009-11-24 11:24:27,656 -- INFO -- JNDI InitialContext properties:{}
org.hibernate.transaction.TransactionManagerLookupFactory 2009-11-24 11:24:27,671 -- INFO -- instantiating TransactionManagerLookup: org.springframework.orm.hibernate3.LocalTransactionManagerLookup
org.hibernate.transaction.TransactionManagerLookupFactory 2009-11-24 11:24:27,671 -- INFO -- instantiated TransactionManagerLookup
org.hibernate.transaction.TransactionManagerLookupFactory 2009-11-24 11:24:27,671 -- INFO -- instantiating TransactionManagerLookup: org.springframework.orm.hibernate3.LocalTransactionManagerLookup
org.hibernate.transaction.TransactionManagerLookupFactory 2009-11-24 11:24:27,671 -- INFO -- instantiated TransactionManagerLookup
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Automatic flush during beforeCompletion(): disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Automatic session close at end of transaction: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- JDBC batch size: 15
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- JDBC batch updates for versioned data: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Scrollable result sets: enabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- JDBC3 getGeneratedKeys(): enabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Connection release mode: auto
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Maximum outer join fetch depth: 2
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Default batch fetch size: 1
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Generate SQL with comments: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Order SQL updates by primary key: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Order SQL inserts for batching: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
org.hibernate.hql.ast.ASTQueryTranslatorFactory 2009-11-24 11:24:27,671 -- INFO -- Using ASTQueryTranslatorFactory
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Query language substitutions: {}
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- JPA-QL strict compliance: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Second-level cache: enabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Query cache: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Cache provider: org.hibernate.cache.NoCacheProvider
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Optimize cache for minimal puts: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Structured second-level cache entries: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Echoing all SQL to stdout
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Statistics: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Deleted entity synthetic identifier rollback: disabled
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Default entity-mode: pojo
org.hibernate.cfg.SettingsFactory 2009-11-24 11:24:27,671 -- INFO -- Named query checking : enabled
org.hibernate.impl.SessionFactoryImpl 2009-11-24 11:24:27,671 -- INFO -- building session factory
org.hibernate.impl.SessionFactoryObjectFactory 2009-11-24 11:24:27,671 -- INFO -- Not binding factory to JNDI, no JNDI name configured
org.hibernate.transaction.JTATransaction 2009-11-24 11:24:27,859 -- ERROR -- Could not find UserTransaction in 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(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:60)
	at org.hibernate.transaction.JTATransactionFactory.createTransaction(JTATransactionFactory.java:57)
	at org.hibernate.jdbc.JDBCContext.registerSynchronizationIfPossible(JDBCContext.java:172)
	at org.hibernate.impl.SessionImpl.checkTransactionSynchStatus(SessionImpl.java:1867)
	at org.hibernate.impl.SessionImpl.getFlushMode(SessionImpl.java:1295)
	at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1184)
	at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
	at com.xa.dao.User1DAO.save(User1DAO.java:34)
	at com.xa.service.UserServiceDS.insertUser1(UserServiceDS.java:24)
	at com.xa.service.UserServiceDS.insertBothUser(UserServiceDS.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	at $Proxy0.insertBothUser(Unknown Source)
	at app.AppMain.main(AppMain.java:27)
com.xa.dao.User1DAO 2009-11-24 11:24:27,875 -- ERROR -- save failed
org.springframework.orm.hibernate3.HibernateSystemException: Could not find UserTransaction in JNDI: ; nested exception is org.hibernate.TransactionException: Could not find UserTransaction in JNDI: 
	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
	at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
	at com.xa.dao.User1DAO.save(User1DAO.java:34)
	at com.xa.service.UserServiceDS.insertUser1(UserServiceDS.java:24)
	at com.xa.service.UserServiceDS.insertBothUser(UserServiceDS.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	at $Proxy0.insertBothUser(Unknown Source)
	at app.AppMain.main(AppMain.java:27)
Caused by: org.hibernate.TransactionException: Could not find UserTransaction in JNDI: 
	at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:64)
	at org.hibernate.transaction.JTATransactionFactory.createTransaction(JTATransactionFactory.java:57)
	at org.hibernate.jdbc.JDBCContext.registerSynchronizationIfPossible(JDBCContext.java:172)
	at org.hibernate.impl.SessionImpl.checkTransactionSynchStatus(SessionImpl.java:1867)
	at org.hibernate.impl.SessionImpl.getFlushMode(SessionImpl.java:1295)
	at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1184)
	at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
	... 17 more
Caused by: 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(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:60)
	... 24 more
Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: Could not find UserTransaction in JNDI: ; nested exception is org.hibernate.TransactionException: Could not find UserTransaction in JNDI: 
	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
	at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
	at com.xa.dao.User1DAO.save(User1DAO.java:34)
	at com.xa.service.UserServiceDS.insertUser1(UserServiceDS.java:24)
	at com.xa.service.UserServiceDS.insertBothUser(UserServiceDS.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	at $Proxy0.insertBothUser(Unknown Source)
	at app.AppMain.main(AppMain.java:27)
Caused by: org.hibernate.TransactionException: Could not find UserTransaction in JNDI: 
	at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:64)
	at org.hibernate.transaction.JTATransactionFactory.createTransaction(JTATransactionFactory.java:57)
	at org.hibernate.jdbc.JDBCContext.registerSynchronizationIfPossible(JDBCContext.java:172)
	at org.hibernate.impl.SessionImpl.checkTransactionSynchStatus(SessionImpl.java:1867)
	at org.hibernate.impl.SessionImpl.getFlushMode(SessionImpl.java:1295)
	at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1184)
	at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)
	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
	... 17 more
Caused by: 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(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
	at javax.naming.InitialContext.lookup(InitialContext.java:392)
	at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:60)
	... 24 more

 我是参照:

http://blog.csdn.net/soleghost/archive/2006/10/13/1332443.aspx

 

进行配置的,可进行事务管理时就报错,我使用JOTM,就是不想使用JNDI的。

 

===================

很久没更新了,不好意思。

 

下面这个配置是可以成功的,各位可以参考下:

 

	<bean id="jotm" class="com....transaction.jta.JotmFactoryBean" />
	<bean id="jotmTXManager" class="com....transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>
	
	<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="..." />
			</bean>
		</property>
	</bean>

    <bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean class="org.enhydra.jdbc.standard.StandardXADataSource">
				<property name="transactionManager" ref="jotm" />
				<property name="driverName" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url" value="jdbc:oracle:thin:@...:1521:..." />
			</bean>
		</property>
	</bean>
	
	<bean id="sessFactory1" class="com......LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="mappingLocations">
            <list>
                <value>classpath:com/**/*.hbm.xml</value>
            </list>
        </property>
       
	</bean>
	
	<bean id="sessFactory2" class="com.....LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="mappingLocations">
            <list>
                <value>classpath:com/**/*.hbm.xml</value>
            </list>
        </property>
       
	</bean>

 

你可能感兴趣的:(JNDI)