spring boot分布式事务错误java.lang.ClassNotFoundException:javax.transaction.TransactionManager

时间:2018-04-30 11:43:33

标签: spring-boot transactions xa atomikos

我正在尝试分布式事务但是我有很多错误,所以我编写了这个演示应用来解释我的问题 我的pom.xml有这些依赖项:

   <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>ch.maxant</groupId>
            <artifactId>genericconnector-atomikos-api</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.4</version>
        </dependency>

    </dependencies>

我唯一包含main函数的类是:

 @Configuration
 public class Main {
    @Autowired
    @Qualifier("xaTransactionManager")
    protected static UserTransactionManager tm;

    @Primary
    @Bean(name = "xaTransactionManager", initMethod = "init", destroyMethod = "close")
    public UserTransactionManager xaTransactionManager() throws SystemException {
        UserTransactionManager txManager = new UserTransactionManager();
        txManager.setTransactionTimeout(300);
        txManager.setForceShutdown(false);
        return txManager;
    }

    @Bean(name = "xaTransactionService", initMethod = "init", destroyMethod = "shutdownWait")
    public UserTransactionServiceImp xaTransactionService() {
        return new UserTransactionServiceImp();
    }



    public static void main(String[] s) {
        try {
            tm.begin();
              // here is empty   
            tm.commit();
        } catch (Exception e) {
            tm.rollback();
            System.out.println(e.getMessage());
        }
    }
 }

我刚刚开始交易  但我在tm.begin()

行遇到了这个问题
java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 19 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" [INFO] NETBEANS-ExecEvent:{"exc":{"msg":"Q29tbWFuZCBleGVjdXRpb24gZmFpbGVkLg=="},"mojo":{"impl":"org.codehaus.mojo.exec.ExecMojo","urls":["file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/codehaus\/mojo\/exec-maven-plugin\/1.2.1\/exec-maven-plugin-1.2.1.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/apache\/maven\/reporting\/maven-reporting-api\/2.0.6\/maven-reporting-api-2.0.6.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/apache\/maven\/doxia\/doxia-sink-api\/1.0-alpha-7\/doxia-sink-api-1.0-alpha-7.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/commons-cli\/commons-cli\/1.0\/commons-cli-1.0.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/codehaus\/plexus\/plexus-interactivity-api\/1.0-alpha-4\/plexus-interactivity-api-1.0-alpha-4.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/codehaus\/plexus\/plexus-utils\/2.0.5\/plexus-utils-2.0.5.jar","file:\/C:\/Users\/NOSIRAT\/.m2\/repository\/org\/apache\/commons\/commons-exec\/1.1\/commons-exec-1.1.jar"],"goal":"exec","id":"org.codehaus.mojo:exec-maven-plugin:1.2.1","source":"CLI","execId":"default-cli"},"type":"MojoFailed"}

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

javax.transaction.TransactionManagerJ2EE SDKjavaee.jar中的一个类,可能您在项目的类路径中缺少此jar文件。

您可以按照this文章为项目添加所需的依赖项! 对于不耐烦的人,这里是需要添加到pom.xml

的依赖项
<repository>
    <id>Java.Net</id>
    <url>http://download.java.net/maven/2/</url>
</repository>

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
</dependency>

希望这有帮助!