gpt4 book ai didi

java - 在 JBoss Weld 中的持久性单元服务之前启动服务

转载 作者:行者123 更新时间:2023-11-30 02:42:42 35 4
gpt4 key购买 nike

我目前有一个应用程序,它使用 JBoss 来启动服务并使用焊接来管理 EJB。当它启动使用自定义类的持久性单元管理器时,@EJB 注释以及服务为空并且尚未被调用。有没有办法设置一个顺序来强制服务在持久性单元之前启动?

最佳答案

我建议在 JBoss 应用程序服务器中配置 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;

}

关于java - 在 JBoss Weld 中的持久性单元服务之前启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171904/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com