CDI + JPA + EJB + JTA + JSF --JBoss-Kitchen Example with PostgreSQL? - java

I am trying to learn the new technologies CDI + JPA + EJB + JTA + JSF so, i have downloaded the sample project from JBoss-Community--> jboss-as-kitchensink(from JBoss in my Eclipse juno).
I am Trying to connect the kitchen with my PostgreSQL database:
My database name is : sampledb
My Table(Member) Structure is : Database name :QUICKSTART_DATABASENAME
CREATE TABLE member
(
id integer,
name text,
email text,
phone_number numeric
);
I have edited this files in my kitchen Project :
persistence.xml
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
I Added the Dependency in pom.xml:
<!-- PostgreSQL Dependency -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
But i'm getting error:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.554s
[INFO] Finished at: Sat Sep 21 14:01:48 IST 2013
[INFO] Final Memory: 20M/175M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jboss.as.plugins:jboss-as-maven-plugin:7.2.Final:deploy (default-cli) on project jboss-as-kitchensink: Deployment f
ailed and was rolled back. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
C:\Users\mypc\java\workspace\jboss-as-kitchensink>
What should i do so that i can run this application with my database to insert the data.
Do i need to change any code in .java files or any .xml file to make it work?
EDITED after Comments: By Following this link,I have configured my PostgreSQL into JBoss manually
by setting up the standalone-full.xml file of JBoss..
----///---- <datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/PostgreSQLDS" pool-name="PostgreSQLpool" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
<driver>org.postgresql.Driver</driver>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="postgresql" module="org.postgresql">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
----////---
Answer :
Install the PostgreSQL driver and use the XML so that u can make it work.
Follow The Steps : (Suggested By #Craig Ringer) is Very HelpFull
Download PgJDBC. I'm assuming you're using postgresql-9.2-1003.jdbc4.jar, the current version at time of writing. Adjust any filenames to match if you need a different version.
Now deploy the JDBC driver to JBoss AS 7 by putting it in the deployments folder or using the deploy command in jboss-cli. This will work for most, but not all, purposes.
Alternately, you an define a PostgreSQL JDBC driver module:
Create the path $JBOSS_HOME/modules/org/postgresql/main. The modules/org part should already exist, make directories for the rest.
In $JBOSS_HOME/modules/org/postgresql/main/modules.xml with the following content, changing the resource-root entry for the PgJDBC driver to refer to the driver you wish to use.
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="org.postgresql">
<resources>
<resource-root path="postgresql-9.2-1003.jdbc4.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
Into the same directory as modules.xml place postgresql-9.2-1003.jdbc4.jar
Open jboss-cli by running $JBOSS_HOME/bin/jboss-cli --connect
Run the command:
/subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)
Now create any required data sources, etc, using postgresql-driver as the driver name.
You can create a datasource via the web ui, with jboss-cli with the data-source create command (see data-source --help, data-source add --help), or by deploying a -ds.xml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<datasource jndi-name="java:/datasources/PostgresqlDS" enabled="true" use-java-context="true"
pool-name="PostgresqlDS">
<connection-url>jdbc:postgresql:dbname</connection-url>
<driver>postgresql-driver</driver>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
</datasource>
</datasources>
For KitchenSink Example: I Tried With These, It Worked till Connection and All the Things.Still It Should be updated After Later...
Follow this link Till Creating a database and a user & continue the rest from here !
Change Persistance To :
persitance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/KitchenQuickStartDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
test-persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/KitchensinkQuickstartTestDS</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
test-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<datasource jndi-name="java:jboss/datasources/KitchenQuickStartTestDS"
pool-name="KitchenQuickStartTestDS" enabled="true"
use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
<driver>postgresql-driver</driver>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
</datasource>
</datasources>
kitchensink-quickstart-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference
this in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/KitchenQuickStartDS"
pool-name="KitchenQuickStartDS" enabled="true"
use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/QUICKSTART_DATABASENAME</connection-url>
<driver>postgresql-driver</driver>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
</datasource>
</datasources>
But Where i'm Stuck is :
I'm Unable to insert the Data Inot the Database! It is raising an error as
ERROR: relation "hibernate_sequence" does not exist Position: 17
I need A Help To Configure It .. I tried in MANY ways

I dont know if you still need help with this but I encounted similar error when deploying my application to the Openshift.
First I connected over SSH to openshift console. Then I ran psql (you can run psql on your host from $POSTGRES_HOME/bin/psql) with credentials for my postgres connection and used \ds and \dt to determine that hibernate_sequence was a table and not a sequence.
So I dropped it: DROP TABLE hibernate_sequence;
And recreated it as a sequence: CREATE SEQUENCE hibernate_sequence;

For the Error Raised as: ERROR: relation "hibernate_sequence" does not exist Position: 17
i had created a SEQUENCE as Suggested by #Martin Nuc.
To Check Whether there is a Sequence in the Database or not use \ds : Lists the Sequences in the database.
CREATE SEQUENCE hibernate_sequence;
Then i had raised with few more errors like -> Type Cast like things then after Clearing those Errors It Worked ! Finally :-)
Thanks for the Support Every1 :)

Alternatively you can also add #Generator instead of being auto, you can add sequence and specify a sequence generator that is used by PostGre which is ${table_name}_${pk}_seq.
Nevertheless, One draw back of doing this is to making your app tightly coupled to sequences.
Another strategy is to make hibernate create that hibernate_sequence if you force it to create DDL.

Related

How to map Datasources in Wildfly using deployment descriptors

My question is: How do I map a datasource to a specific jndi-name configured inside wildfly so that multiple deployed .war-files each can use their own specific datasource. The mapping should take place at deployment so that a configuration inside wildfly and the specific project is sufficient.
We got a project which supports multi-tenancy. The structure is as follows:
customerSpecificProject
|-- ui (generic)
|----database (generic)
|----services (generic)
|----etc...
In the database-project a standard datasource is specified which goes by a standard jndi-name java:/xyzDS. Since we migrated from tomcat to wildfly we want to make use of the ability to host multiple applications in our AS.
To achieve this we have to map the java:/xyzDS to the datasources defined in the standalone.xml:
<subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<datasource jta="true" jndi-name="java:/customer1DS" pool-name="c1DS" enabled="true" use-ccm="true">
...
</datasource>
<datasource jta="true" jndi-name="java:/customer2DS" pool-name="c2DS" enabled="true" use-ccm="true">
...
</datasource>
Therefore we tried to use the jboss-web.xml located in the WEB-INF folder of our customerProject:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.jboss.com/xml/ns/javaee
http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
<resource-ref>
<res-ref-name>java:/xyzDS</res-ref-name>
<jndi-name>java:/customer1DS</jndi-name>
</resource-ref>
</jboss-web>
persistence.xml inside the database-Project:
<?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://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="xyzDB" transaction-type="JTA">
<jta-data-source>java:/xyzDS</jta-data-source>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>...</class>
...
<class>...</class>
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<property name="eclipselink.weaving" value="static" />
</properties>
</persistence-unit>
</persistence>
It does not seem to be working. I talked to a colleague who had a similar issue and resolved it in the corresponding way for a websphere AS. I do not know if the problem resides in the structure having a nested databaseProject inside another lib (ui) for the actual customerProject or if it is caused by bad configuration.
Also we use the #Resource Annotation inside some DAOs:
#Resource(lookup = "java:/xyzDS")
private DataSource dataSource;
Maybe the mapping works but not for the annotations inside the compiled DAOs? But I do not understand why that should not work.
Current Error in Stacktrace:
11:18:20,839 ERROR [org.jboss.as.controller.management-operation] (DeploymentScanner-threads - 1) WFLYCTL0013: Operation ("full-replace-deployment") failed - address: ([]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"xyz.war\".INSTALL" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"xyz.war\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"xyz.war\"
Caused by: java.lang.IllegalArgumentException: WFLYEE0047: Incompatible conflicting binding at java:/xyzDS source: lookup (java:/customer1DS)"},
Edit: Already tried adding:
<property name="wildfly.jpa.twophasebootstrap" value="false"/>
To persistence.xml
Have you checked the question/answer in wildfly-development.1055759.n5.nabble.com? In this exchange, it was determined that the persistence unit needed a hint in the form of
<property name="wildfly.jpa.twophasebootstrap" value="false"/>
I don't know if this is applicable in your case, but is worth looking at.

Java web service missing hibernate.dialect

I am trying to create web service with creation to oracle database. As a server I use Wildfly so I initialized JNDI connection using Administrative console in browser.
Standalone.xml
{
...
<datasource jta="false" jndi-name="java:/jdbc/ora" pool-name="ora" enabled="true" use-ccm="false">
<connection-url>jdbc:oracle:thin:#localhost:1522:maindatabase</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>ora</driver>
<security>
<user-name>C##Edgar</user-name>
<password>2301910</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
...
}
But after creating the JPA project and adding <jta-data-source> in persistance.xml it throws me an exception when I try to create .ear file and deploy project.
Persistance.xml
<?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="Model">
<jta-data-source>java:/jdbc/ora</jta-data-source>
</persistence-unit>
</persistence>
Exception is:
JBAS014777: Services which failed to start: service jboss.persistenceunit."App1.ear#Model"
service jboss.persistenceunit."App1.ear#Model": org.jboss.msc.service.StartException in service jboss.persistenceunit."App1.ear#Model": org.hibernate.HibernateException: Unable to determine Dial
ect to use [name=Oracle, majorVersion=12]; user must register resolver or explicitly set 'hibernate.dialect'
Maybe someone can tell me what I missed or done wrong?

Upgrade to JBoss 7 Datasource is Not Working

I am trying to upgrade to JBoss 7; however, my datasource is not creating a connection. I did not deploy any WAR files. I am testing the connection by using the admin console. In standalone.xml, I configured the datasource as follows:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jta="false" jndi-name="java:jboss/projectDS" pool-name="projectDS" enabled="true">
<connection-url>jdbc:oracle:thin:#mcc-67-150.usae.ABC.com:1521:test</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>ojdbc6</driver>
<security>
<user-name>XXX</user-name>
<password>XXX</password>
</security>
</datasource>
<drivers>
<driver name="ojdbc6" module="com.oracle" />
</drivers>
</datasources>
</subsystem>
The ojdbc6.jar driver is in the following folder %JBOSS_HOME%\modules\com\oracle\main. The entry in my module.xml reads as follows:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.oracle">
<resources>
<resource-root path="ojdbc6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
The error that I receive in my server log when I test the connection from the admin console is:
07:47:11,863 WARN [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool]
(HttpManagementService-threads - 4) IJ000604: Throwable while attempting to get
a new connection: null: javax.resource.ResourceException: Could not create conne
ction
at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLo
calManagedConnection(LocalManagedConnectionFactory.java:277) [ironjacamar-jdbc-1
.0.9.Final.jar:1.0.9.Final]
Thanks in advance!
Deploy the oracle driver jar to jboss from the console. Its something like localhost:9990/console with credential test/test123
From the datasource option, check whether you can see your datasource java:jboss/hqiis over there or not. If not, try to create the datasource from the console instead of configuring through xml.
Also check the data source status from the jboss-cli:
a. From command prompt, enter into JBOSS_HOME\bin
b. Type connect YOUR_IP:YOUR_PORT (example 192.169.1.10:9999)
c. Type this to see whether your datasource is available or not:
/subsystem=datasources/data-source=hqiis:test-connection-in-pool
You should see a msg like below if you are having a configured datasource named hqiis
connection-in-pool
{
"outcome" => "success",
"result" => [true]
}

JBoss 7 missing jboss.naming.context.java - can't run EAR

I'm trying to run an ear on jboss 7 and everytime I run the ear I get this error:
"Services with missing/unavailable dependencies" =>
["jboss.persistenceunit.\"PROJECTNAME.jar#PROJECTNAMEPU\" missing [
jboss.naming.context.java.PROJECTNAME ]"]
Also before this error I get this feedback for every session/entity bean;
INFO
[org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor]
(MSC service thread 1-8) JNDI bindings for session bean named
SESSIONBEAN in deployment unit subdeployment "PROJECTNAME.war" of
deployment "PROJECTNAME.ear" are as follows:
I tried looking it up and idk what I'm missing, any suggestions or help??
UPDATE:
In my standalone.xml for jboss
<datasource jndi-name="java:jboss/datasources/PROJECTNAME" enabled="true" use-java-context="true" pool-name="PROJECTNAME" use-ccm="false">
<connection-url>jdbc:oracle:thin:#SERVER:PORT:support</connection-url>
<driver>ojdbc14.jar</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>USERNAME</user-name>
<password>PASS</password>
</security>
</datasource>
<xa-datasource jndi-name="java:jboss/datasources/PROJECTNAME" pool-name="PROJECTNAME">
<driver>ojdbc14.jar</driver>
<xa-datasource-property name="URL">jdbc:oracle:thin:#SERVER</xa-datasource-property>
<xa-pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
</xa-pool>
<security>
<user-name>USERNAME</user-name>
<password>PASS</password>
</security>
</xa-datasource>
<drivers>
<driver name="oracle" module="com.oracle.ojdbc14">
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
My persitence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="PROJECTNAMEPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/PROJECTNAME</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Looks like your EAR requires some JNDI bindings to be configured in JBoss, likely DataSources for use by JPA. Java EE applications can have dependencies on services provided by the container and those are usually looked up using the naming service (JNDI).
Check the deploy/install instructions that come with the EAR you're trying to deploy, or refer back to whoever provides the application. Unfortunately it's not possible to provide much more help without much more info.

multiple DBs with Jboss 4.2.3

We are using Jboss 4.2.3 with EJBs and hibernate.
Currently we defined 3 db connections in our persistence.xml file (one for reads (tx-local), one for writes (no-tx) and one for statistics (no-tx)), and when we add a forth db connection which is also (no-tx) and map a couple of entities to it, the machine starts to show an unusual load.
If we remove this connection (no one is using it still) the load get back to normal.
I guess it is some sort of configuration problem with JBoss, but I'm not sure.
any assistance will be appreciated.
thanks.
this is the ds.xml file content:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: feedback-ds.xml 71535 2008-04-01 07:05:03Z adrian#jboss.org $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<no-tx-datasource>
<jndi-name>FeedbackDS</jndi-name>
<connection-url>jdbc:mysql://m6sdb:3306/m6sFeedbacks?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>password</password>
<min-pool-size>10</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>1000</max-pool-size>
<property name="hibernate.show.sql" value="true"></property>
<idle-timeout-minutes>10</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support -->
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</no-tx-datasource>
</datasources>

Categories