4

我在为 OpenEJB 3.1.3 正确配置数据源时遇到问题。我尝试配置与 postgres db 的连接,但是当我调试测试时,我得到了默认的 hsql 连接参数。

这是我的测试课:

@RunWith(ApplicationComposer.class)
public class OpenEJBTest {
    @EJB
    Files fb;

    @Test
    public void testSth() {
        List<UploadSession> uploadNotFinishedSessions = fb.getUploadNotFinishedSessions();
    }

    @Module
    public EjbJar beans() {
        EjbJar ejbJar = new EjbJar("beans");
        ejbJar.addEnterpriseBean(new StatelessBean(FilesBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(FilesUtilBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(ServerFilesBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(UserUtilBean.class));
        return ejbJar;
    }

    @Module
    public PersistenceUnit persistence() {
        PersistenceUnit unit = new PersistenceUnit("postgresPU", HibernatePersistence.class.getName());
        String simpleXml = SimpleXmlUtil.toSimpleXml(unit);
        System.out.println(simpleXml);
        unit.setJtaDataSource("PostgresDS");
        unit.setNonJtaDataSource("PostgresDSUnmanaged");
        return unit;
    }
}

我尝试过了:

  1. 将 jndi.properties 添加到类路径:
postgresPU=new://Resource?type=DataSource
postgresPU.JdbcDriver=org.postgresql.Driver
postgresPU.JdbcUrl=jdbc:postgresql:/localhost:5433/pdb
postgresPU.JtaManaged=true
postgresPU.DefaultAutoCommit=false
postgresPU.UserName=...
  1. 通过 openejb.xml 配置数据源(位于类路径中)
<Resource id="PostgresDS" type="DataSource">
  JdbcDriver  org.postgresql.Driver
  JdbcUrl   jdbc:postgresql://localhost:5433/pdb
  UserName   user
  Password  pass
  JtaManaged true
</Resource>

 <Resource id="PostgresDSUnmanaged" type="DataSource">
     JdbcDriver  org.postgresql.Driver
    JdbcUrl     jdbc:postgresql://localhost:5433/pdb
     UserName    user
    Password    pass
  JtaManaged  false
 </Resource>

但它都不起作用 - 根本没有配置数据源,数据源的默认版本仍然存在。由于默认的 hsql 连接,我得到以下错误:

WARN - SQL Error: -22, SQLState: S0002
ERROR - Table not found in statement [select top ? user0_.id as col_0_0_ from tusers user0_ where user0_.login=?]

我可能做错了什么?

4

1 回答 1

5

ApplicationComposer不使用 JNDI 引导容器,因此jndi.properties永远不会看到该文件。

你可以做的是使用org.apache.openejb.junit.Configuration方法上的注释让它返回你想用来配置测试的属性。

@Configuration
public Properties properties() {
   //...
}

我将重命名jndi.properties为其他名称,以免造成混淆,然后您可以使用这样的代码在类路径中找到它。

   final URL url = this.getClass().getResource("/config.properties");
   final Properties props = new Properties();
   final InputStream in = url.openStream();
   try {
       props.load(in);
   } finally {
       close(in);
   }
于 2012-06-05T23:05:52.997 回答