我目前有一个使用JBoss启动服务并使用焊接管理EJB的应用程序。当它开始使用自定义类的持久性单元管理器时,@ EJB注释以及服务为空并且尚未被调用。有没有办法设置一个强制服务在持久性单元之前启动的命令?
答案 0 :(得分:2)
我建议在JBoss Application Server中配置JTA数据源,以便监控池化连接对象及其用法。
将此数据复制到standalone.xml
中的数据源子系统中:
注意:在使用此数据源之前,您需要注册供应商数据库服务器的jdbc驱动程序。
Adding Database Driver in JBoss and Wildfly.
<datasource jta="true" jndi-name="java:/datasource/YOUR_APPLICATION_NAME" pool-name="YOUR_APPLICATION_NAME" enabled="true" use-ccm="true" statistics-enabled="true"><!--JNDI Name which will be used in your persistence.xml in your applicaton-->
<connection-url>jdbc:mysql://server-ip-here:3306/your-database-name</connection-url><!-- change according to your database-->
<driver-class>com.mysql.jdbc.Driver</driver-class><!-- change according to your database-->
<connection-property name="zeroDateTimeBehavior">
convertToNull
</connection-property>
<driver>mysql</driver><!-- change according to your database-->
<new-connection-sql>SELECT 1</new-connection-sql>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>3</min-pool-size>
<initial-pool-size>3</initial-pool-size>
<max-pool-size>5</max-pool-size>
<prefill>true</prefill>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<user-name>db-user-name</user-name>
<password>db-user-password</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/><!-- change according to your database-->
<check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>false</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/><!-- change according to your database-->
</validation>
<timeout>
<blocking-timeout-millis>6000</blocking-timeout-millis>
<idle-timeout-minutes>30</idle-timeout-minutes>
<query-timeout>60</query-timeout>
<allocation-retry>4</allocation-retry>
<allocation-retry-wait-millis>200</allocation-retry-wait-millis>
</timeout>
<statement>
<track-statements>true</track-statements>
</statement>
</datasource>
您的src/main/java/resources/META-INF/persistence.xml
文件应如下所示:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myAppPu" transaction-type="JTA">
<description>Persistence Unit for MyApp Database</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc:mysql://server-ip-here:3306/your-database-name</jta-data-source><!--This is already defined in newly created datasource-->
!--<validation-mode>CALLBACK</validation-mode>-->
<properties>
<property name="javax.persistence.schema-generation.create-database-schemas" value="false" />
<property name="javax.persistence.schema-generation.database.action" value="none" />
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="false"></property>
<property name="use_sql_comments" value="false" />
<property name="hibernate.archive.autodetection" value="class"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"></property>
<!-- Hibernate Envers -->
<!--<property name="hibernate.auditable" value="false" />-->
</properties>
</persistence-unit>
</persistence>
现在您已准备好在应用程序中使用EntityManager。 用法示例:
@Stateless
public class MyEjbServiceImpl implements MyEjbService {
@PersistenceContext(unitName = "myAppPu") //this should match with unitname defined in persistence.xml
private EntityManager em;
}