简体   繁体   English

JBoss 7和EJB3 | 我应该在哪里定义数据源

[英]JBoss 7 & EJB3 | Where should I define datasource

I'm new to Jboss and I don't understand where should I define the database connection data like url, username, password etc.. 我是Jboss的新手,我不知道该在哪里定义数据库连接数据,例如url,用户名,密码等。

Here is my multimodule project: 这是我的多模块项目:

app-root
    app-api
      - src
      - pom.xml

    app-ear
      - src
      - pom.xml

    app-ejb
      - src
      - pom.xml

    pom.xml

My persinstence.xml located unter app-root/app-ejb/src/main/config/default/META-INF/persistence.xml 我的persinstence.xml位于app-root/app-ejb/src/main/config/default/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="ejb3_jpa_myapp_pu" transaction-type="JTA">
        <description>Jboss Test application</description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jdbc/MyApp</jta-data-source>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="none" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>

And finaly my simple Service: 最后,我简单的服务:

@Stateless
@Remote(IService.class)
public class ServiceImpl implements IService{

    @PersistenceContext(unitName = "ejb3_jpa_myapp_pu")
    private EntityManager em;

    @Override
    public void doSomeJob() {
        // [...]
    }
}

I know I have to define the database connection properties but where can I do that? 我知道我必须定义数据库连接属性,但是在哪里可以做?

You need to define the data source in the standalone XML file that your JBoss instance is using. 您需要在JBoss实例正在使用的独立XML文件中定义数据源。 JBoss does not ship with database drivers so there are two steps: JBoss不附带数据库驱动程序,因此有两个步骤:

1) Create a JBoss module for the database drivers (eg MySQL, Oracle, Postgres etc) 1)为数据库驱动程序创建一个JBoss模块(例如MySQL,Oracle,Postgres等)

2) Create the datasource definition 2)创建数据源定义

Step 1) needs only to be done once, ie many MySQL datasources can use the same MySQL JBoss module. 步骤1)只需要执行一次,即许多MySQL数据源可以使用相同的MySQL JBoss模块。

Step 2) An example datasource configuration for MySQL would be: 步骤2)MySQL的示例数据源配置为:

<datasources>
   <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS">
      <connection-url>jdbc:mysql://localhost:3306/EJB3</connection-url>
         <driver>com.mysql</driver>
      <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
      <pool>
        <min-pool-size>10</min-pool-size>
        <max-pool-size>100</max-pool-size>
        <prefill>true</prefill>
      </pool>
      <security>
        <user-name>test</user-name>
        <password>test</password>
      </security>
      <statement>
        <prepared-statement-cache-size>32</prepared-statement-cache-size>
        <share-prepared-statements/>
      </statement>
    </datasource>
    <drivers>
      <driver name="com.mysql" module="com.mysql">
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
      </driver>
    </drivers>
</datasources>

The above can be automated using the JBoss CLI. 可以使用JBoss CLI自动执行以上操作。 Also creating the JBoss modules can also be automated for example using the smartics-jboss-modules-maven-plugin see Generate an xml file with all dependencies with maven for more info. 例如,也可以使用smartics-jboss-modules-maven-plugin自动创建JBoss模块有关更多信息,请参见使用maven生成具有所有依赖关系的xml文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM