Posted on 2009-04-08 09:10
landor 阅读(2107)
评论(0) 编辑 收藏 所属分类:
ibatis
用
JDBC方式基本配置都很简单,weblogic的数据源JNDI名字是:TestDatasource
那么ibatis的SqlMapConfig.xml的配置如下
<transactionManager type="JDBC" >
<dataSource type="JNDI">
<property name="context.java.naming.factory.initial" value="weblogic.jndi.WLInitialContextFactory" />
<property name="context.java.naming.provider.url" value="t3://localhost:7001" />
<property name="DataSource" value="TestDatasource" />
</dataSource>
</transactionManager>
此时启动weblogic服务器,进行正常junit测试即可(这里需要把weblogic.jar加到classpath中)
然而如果用如下的配置方式
<transactionManager type="JTA" >
<property name="UserTransaction" value="javax.transaction.UserTransaction"/>
<dataSource type="JNDI">
<property name="context.java.naming.factory.initial" value="weblogic.jndi.WLInitialContextFactory" />
<property name="context.java.naming.provider.url" value="t3://localhost:7001" />
<property name="DataSource" value="TestDatasource" />
</dataSource>
</transactionManager>
也就是用JTA来做事务,那么会出现错误
java.lang.ExceptionInInitializerError
at gmt.boss.obs.account.recurringcharge.IbatisTest.setUp(IbatisTest.java:12)
at junit.framework.TestCase.runBare(TestCase.java:128)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.RuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/transactionManager/end()'. Cause: com.ibatis.sqlmap.client.SqlMapException: Error initializing JtaTransactionConfig while looking up UserTransaction (javax.transaction.UserTransaction). Cause: 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 com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:89)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)
at testIbatis.IbatisInstance.<clinit>(IbatisInstance.java:27)
12 more
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/transactionManager/end()'. Cause: com.ibatis.sqlmap.client.SqlMapException: Error initializing JtaTransactionConfig while looking up UserTransaction (javax.transaction.UserTransaction). Cause: 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 com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:86)
14 more
Caused by: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/transactionManager/end()'. Cause: com.ibatis.sqlmap.client.SqlMapException: Error initializing JtaTransactionConfig while looking up UserTransaction (javax.transaction.UserTransaction). Cause: 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 com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:105)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)
15 more
Caused by: com.ibatis.sqlmap.client.SqlMapException: Error initializing JtaTransactionConfig while looking up UserTransaction (javax.transaction.UserTransaction). Cause: 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 com.ibatis.sqlmap.engine.transaction.jta.JtaTransactionConfig.initialize(JtaTransactionConfig.java:50)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$6.process(SqlMapConfigParser.java:306)
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121)
19 more
我已经在配置文件里配置了context.java.naming.factory.initial,而在这里还提示这个错误
关键看这里
Error initializing JtaTransactionConfig while looking up UserTransaction (javax.transaction.UserTransaction). Cause: 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
在jvm里加上一个参数,测试一下
-Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
运行通过
这个参数在:右键项目--Run as --Open Run Dialog--Arguments的VM arguments中添加即可
引入ibatis帮助的一段话,如下:
警告!虽然这些看起来很简单,但记住不要滥用全局(分布式)事务,这点很重要。
这样做既有性能方面的考虑,同时也是因为全局事务会让应用服务器和数据库驱动程序的设置变得更复杂。
虽然看起来简单,您可能还是会遇到一些困难。记住,EJB拥有更多厂商和工具支持。
对于需要分布式事务的应用,最好还是使用Session EJB