没有Spring的Atomikos事务管理器

时间:2016-08-20 10:29:20

标签: java spring hibernate jpa

我试图在不使用spring的情况下配置Atomikos事务。首先我试图在不使用spring的情况下设置EntityManagerFactory以下是我试过的代码

private static AtomikosDataSourceBean prepareDataSource(){
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("demo");
        atomikosDataSourceBean.setXaDataSourceClassName("oracle.jdbc.xa.client.OracleXADataSource");
        Properties properties = new Properties();
        properties.setProperty("user", "demo");
        properties.setProperty("password", "demo");
        properties.setProperty("URL", "dbc:oracle:thin:@localhost:1521/xe");
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;
    }


    public static EntityManagerFactory getEntityManagerFactory(){
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(prepareDataSource());
        entityManagerFactory.setPersistenceUnitName("demo");
        entityManagerFactory.setPersistenceXmlLocation("classpath*:META-INF/persistence.xml");
        Properties properties = new Properties();
        properties.setProperty("hibernate.transaction.jta.platform", "com.demo.AtomikosJtaPlatform");
        properties.setProperty("hibernate.show_sql", "true");
        return  (EntityManagerFactory) entityManagerFactory;

    }

上面的代码给我一个classcastexception.How如何在不使用spring的情况下得到相同的entitymanager;

1 个答案:

答案 0 :(得分:1)

我会参考Atomikos的官方文档,它实际上包含了一个选择不使用Spring的人的例子:

Atomikos without Spring