线程CMP上没有tx OpenEJB4 JUnit Hibernate

时间:2012-05-25 02:53:30

标签: java hibernate junit openejb

我一直收到以下异常,

java.lang.IllegalStateException: No tx on thread at  org.apache.geronimo.transaction.manager.TransactionManagerImpl.getActiveTransactionImpl(TransactionManagerImpl.java:201)
    at org.apache.geronimo.transaction.manager.TransactionManagerImpl.getResource(TransactionManagerImpl.java:194)
    at org.apache.openejb.core.transaction.JtaTransactionPolicy.getResource(JtaTransactionPolicy.java:111)
    at org.apache.openejb.core.transaction.EjbTransactionUtil.afterInvoke(EjbTransactionUtil.java:76)
    at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:246)
    at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:178)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:260)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:240)
    at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:91)
    at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:284)
    at $Proxy70.call(Unknown Source)
    at com.xyz.cms.epgmgmt.entitymanager.ProviderManagerTest.testWithTransaction(ProviderManagerTest.java:66)
    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 junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我正在使用OpenEJB4进行JUnit测试。

EJB本身是无状态的,没有事务注释(这使得需要Transaction)。我在这个论坛上发现了一个较旧的帖子并尝试了相同的修复,但没有奏效。我的类路径上没有hibernate验证。我正在使用Hibernate 3。

我的测试如下,

public class ProviderManagerTest extends TestCase {


Context context;

    /**
 * Bootstrap the Embedded EJB Container
 *
 * @throws Exception
 */
protected void setUp() throws Exception {


Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
p.put("log4j.category.OpenEJB.options ", " debug");

p.put("RcsDB", "new://Resource?type=DataSource");
p.put("RcsDB.JdbcDriver", "oracle.jdbc.driver.OracleDriver");
p.put("RcsDB.JdbcUrl", "jdbc:oracle:thin:@dbserver:1521:ttv");
p.put("RcsDB.JtaManaged", "true");
p.put("RcsDB.JtaManaged", "true");
p.put("RcsDB.username", "username");
p.put("RcsDB.password", "password");

context = EJBContainer.createEJBContainer(p).getContext();
context.bind("inject", this);

}

@Test
public void testWithTransaction() throws Exception {
Caller transactionalCaller = (Caller) context.lookup("java:global/cms_epgmgmt/TransactionBean");
transactionalCaller.call(new Callable() {
        public Object call() throws Exception {
        Provider testProvider = new Provider();
    testProvider.setName("test");
    IProviderManager providerManager = null;
    providerManager = (IProviderManager) context.lookup("java:global/cms_epgmgmt/ProviderManager");
    providerManager.create(testProvider);
            return null;
        }
    });
}

    /*private void testProviderCreation()
    {
            Provider testProvider = new Provider();
            testProvider.setName("test");
            providerManager.create(testProvider);
    }
    */

     public static interface Caller {
            public <V> V call(Callable<V> callable) throws Exception;
        }
     /**
 * This little bit of magic allows our test code to execute in
 * the scope of a container controlled transaction.
 */
@Stateless
@TransactionAttribute(REQUIRES_NEW)
public static class TransactionBean implements Caller {

    public <V> V call(Callable<V> callable) throws Exception {
        return callable.call();
    }
}

} 提供者经理如下,     @local(IProviderManager.class)     @Stateless     公共类ProviderManager实现IProviderManager {

    @PersistenceContext(unitName = "epgmanagerjpaunit")
    private EntityManager entityManager;



    public void create(Provider provider)
    {
            entityManager.persist(provider);
    }
}

提前致谢, -v-

0 个答案:

没有答案