0

I'm trying to learn how to work with JavaEE/EJB and database persistence, I have a basic example where I want to save a String to a database via input field and read the list of saved items.

I have a MySQL server installed on localhost (V5.7 Community Edition) and my test server is WildFly 10.1.0 (via Eclipse). The whole project is an EAR container containing a Web and EJB Subproject.

I am using container managed transactions, as I understand it, transactions are automatically created when a method is called and flushed/committed as soon as the method exits.

The problem is, that no data is ever written to the actual database. But no errors are thrown either. I assume, the entity manager caches all supposed saves and directly reads them back on select, without even checking the database. As such, when I restart the server, nothing remains. Also, when I look into mysql db directly, nothing is there either (even while wildfly server is running, directly after supposed insert). I also tried adding some rows to db table directly, but select does not "see" them either.

As a result, it seemed to me that the database is not even accessed, despite it being configured in the persistence.xml. I tried to remove connection url/username/password there and it actually made no difference, the entity manager still throws no errors and everything seems to work. So where exactly is it saving the data and what do I have to change so that it accesses the supplied mysql database instead?

Handler (in Web Project):

@ManagedBean(name = "handlerBean")
@SessionScoped
public class HandlerBean {

@EJB
private TodoWorkerBeanRemote worker;

private String input;

public void add() {
    if (input.compareTo("") != 0) {

        TodoBean item = new TodoBean();
        item.setText(input);
        worker.saveItem(item);

        input = "";
    }
}
...

Session Bean / Transaction container (in EJB project)

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class TodoWorkerBean implements TodoWorkerBeanRemote {

@PersistenceContext(unitName = "EnterpriseTestEJB")
private EntityManager entityManager;

public TodoWorkerBean() {
}

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // does not help
public void saveItem(TodoBean item) {

    // entityManager.joinTransaction(); <- does not help
    entityManager.persist(item); // tried .merge() as well
    // entityManager.flush(); <-does not help
}
...

Entity Bean (in EJB project)

@Entity
@Table(name = "todobean")
@NamedNativeQueries({
    @NamedNativeQuery(name = "TodoBean.getItems", query = "select * from todobean", resultClass = TodoBean.class),
    @NamedNativeQuery(name = "TodoBean.clearItems", query = "delete from todobean") })
public class TodoBean implements Serializable {

private Integer id;
private String text;
...

persistence.xml (tried with hibernate.cfg.xml as well, no difference)

<?xml version="1.0" encoding="UTF-8"?>
<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="EnterpriseTestEJB">
    <class>de.dianasalsa.ejb.TodoBean</class>
    <properties>

        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/feedback" /> // not used
        <property name="hibernate.connection.username" value="root" /> // not used
        <property name="hibernate.connection.password" value="root" /> // not used

        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.use_sql_comments" value="true" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" /> // tried "create" as well

    </properties>

</persistence-unit>
</persistence>

Log:

....
15:42:59,602 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 66) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'EnterpriseTest.ear/EnterpriseTestEJB.jar#EnterpriseTestEJB'
15:42:59,952 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 65) WFLYCLINF0002: Started client-mappings cache from ejb container
15:42:59,973 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 66) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
15:43:00,102 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 66) Envers integration enabled? : true
15:43:00,101 INFO  [org.jboss.as.protocol] (management I/O-1) WFLYPRT0057:  cancelled task by interrupting thread Thread[management-handler-thread - 3,5,management-handler-thread]
15:43:00,604 INFO  [org.hibernate.tool.hbm2ddl.SchemaUpdate] (ServerService Thread Pool -- 66) HHH000228: Running hbm2ddl schema update
15:43:00,617 INFO  [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 66) HHH000262: Table not found: todobean
15:43:00,619 INFO  [org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl] (ServerService Thread Pool -- 66) HHH000262: Table not found: todobean
15:43:01,499 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 83) Mojarra 2.2.13.SP1 20160303-1204 für Kontext '/EnterpriseTestWeb' wird initialisiert.
15:43:02,379 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 83) WFLYUT0021: Registered web context: /EnterpriseTestWeb
15:43:02,414 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "EnterpriseTest.ear" (runtime-name : "EnterpriseTest.ear")
15:43:02,515 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
15:43:02,519 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
15:43:02,519 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 9581ms - Started 879 of 1127 services (422 services are lazy, passive or on-demand)
15:43:18,799 INFO  [org.jboss.ejb.client] (default task-2) JBoss EJB Client version 2.1.4.Final
15:43:32,763 INFO  [stdout] (default task-3) Hibernate: 
15:43:32,763 INFO  [stdout] (default task-3)     /* insert de.dianasalsa.ejb.TodoBean
15:43:32,763 INFO  [stdout] (default task-3)         */ insert 
15:43:32,763 INFO  [stdout] (default task-3)         into
15:43:32,763 INFO  [stdout] (default task-3)             todobean
15:43:32,763 INFO  [stdout] (default task-3)             (text) 
15:43:32,763 INFO  [stdout] (default task-3)         values
15:43:32,764 INFO  [stdout] (default task-3)             (?)

15:43:32,786 INFO  [stdout] (default task-3) Hibernate: 
15:43:32,786 INFO  [stdout] (default task-3)     /* TodoBean.getItems */ select
15:43:32,786 INFO  [stdout] (default task-3)         * 
15:43:32,787 INFO  [stdout] (default task-3)     from
15:43:32,787 INFO  [stdout] (default task-3)         todobean

1 Answers1

0

sorry for my english...

you need to configure a JNDI name "jta-data-source", and say with "hibernate.hbm2ddl.auto" create new relations, like this example:

<?xml version="1.0" encoding="UTF-8"?>
<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="EnterpriseTestEJB"
        transaction-type="JTA">
        <jta-data-source>java:jboss/datasources/some-name</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

wildfly has a "namesever" for that you work with the JNDI name to access from your annotation like "@PersistenceContext".

And in Wildfly at selfe you must configure a new MySql connection with that JNDI name.

For MySql connection download the jdbc driver and upload it to WildFly. https://dev.mysql.com/downloads/connector/j/5.1.html

Inser MySqgl jdbc Driver

The connection will named with the JNDI name (java:jboss/datasources/some-name).

Add new MySql Connection (1)

Add new MySql Connection (2)

When you run your programm in Wildfly the programm will look over the JNDI name in wildfly for your MySql connection.

If you would like to look an example code i have one from my study here: https://gitlab.com/Java_Project/JavaEE2.0

I'll hope it fixe your problem.