给出ClassCastException的Atomikos AtomikosJTATransactionFactory

时间:2011-08-12 15:00:57

标签: classcastexception atomikos

我试图在我的代码中实现Atomikos事务管理器,这是在Hiberate之上的JPA。我收到了这个错误。任何帮助高度赞赏。:

    Caused by: java.lang.ClassCastException: com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
at org.hibernate.transaction.TransactionFactoryFactory.buildTransactionFactory(TransactionFactoryFactory.java:65)
at org.hibernate.cfg.SettingsFactory.createTransactionFactory(SettingsFactory.java:442)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:164)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2159)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1383)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:891)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)

这些是我的配置:

Persistence.xml

<persistence-unit name="persistenceUnit"
    transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>xaDataSource</jta-data-source>
    <properties>
        <property name="hibernate.transaction.manager_lookup_class"
            value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
    </properties>
</persistence-unit>

持久-config.xml中

<bean id="xaDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    init-method="init" destroy-method="close">
    <property name="uniqueResourceName" value="dbResource" />
    <property name="xaDataSourceClassName" value="oracle.jdbc.xa.client.OracleXADataSource" />
    <property name="xaProperties">
        <props>
            <prop key="serverName">${jdbc.server}</prop>
            <prop key="portNumber">1521</prop>
            <prop key="databaseName">${jdbc.database}</prop>
            <prop key="user">${jdbc.user}</prop>
            <prop key="password">${jdbc.password}</prop>
            <prop key="driverType">4</prop>
            <prop key="URL">${jdbc.url}</prop>
        </props>
    </property>
    <property name="maxPoolSize" value="60" />
    <property name="minPoolSize" value="1" />
</bean>


<bean id="pum"
    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath:META-INF/persistence.xml</value>
        </list>
    </property>
    <property name="dataSources">
        <map>
            <entry key="xaDataSource" value-ref="xaDataSource" />
        </map>
    </property>

</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="pum" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database">
                <value>ORACLE</value>
            </property>
            <property name="databasePlatform">
                <value>${hibernate.dialect}</value>
            </property>
            <property name="showSql">
                <value>${hibernate.show_sql}</value>
            </property>
            <property name="generateDdl">
                <value>false</value>
            </property>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
            <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
            <prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory</prop>
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
            <prop key="hibernate.current_session_context_class">jta</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="forceShutdown" value="false" />
</bean>

<bean id="userTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout">
        <value>100</value>
    </property>
</bean>

<bean id="JtaTransactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="transactionManager" />
    <property name="userTransaction" ref="userTransaction" />
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<!-- Activates the scanning of transaction like @transaction -->
<tx:annotation-driven transaction-manager="JtaTransactionManager" />

<!-- Translate native JPA Exceptions into Spring's DataAccessException hierarchy -->
<bean
    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

1 个答案:

答案 0 :(得分:2)

找到解决方案。我的项目已经有Hibernate依赖。我在POM中编写了这个依赖项,用Atomikos排除了Hibernate。我最好的猜测是问题不是Class Cast异常,而是版本冲突:

<dependency>
    <groupId>com.atomikos</groupId>
        <artifactId>transactions-hibernate3</artifactId>
        <version>${atomikos.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>hibernate</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
        </exclusions>
</dependency>