Friday, November 4, 2016

My errors #1 Unable to create connection, postgresql_Driver_9_4 is already registered

After all I'm recieving the errors:
 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 74) javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:jboss/datasources/PostgresDS  

Caused because previous warnings:
 2016-11-04 23:56:27,637 WARN [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0003: Unable to instantiate driver class "org.postgresql.Driver": org.jboss.msc.service.DuplicateServiceException: Service jboss.jdbc-driver.catshelter-java_war_org_postgresql_Driver_9_4 is already registered  

 WARN [org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (ServerService Thread Pool -- 70) IJ000604: Throwable while attempting to get a new connection: null: javax.resource.ResourceException: IJ031084: Unable to create connection  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:345)  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:352)  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:287)  
      at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.createConnectionEventListener(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:1320)  
      at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:496)  
      at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:617)  
      at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:589)  
      at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:590)  
      at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:429)  
      at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:747)  
      at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:138)  
      at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:66)  
      at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)  
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180)  
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68)  
      at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)  
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)  
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)  
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)  
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)  
      at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)  
      at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)  
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)  
      at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)  
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)  
      at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)  
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)  
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)  
      at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)  
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:161)  
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)  
      at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)  
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:193)  
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
      at java.lang.Thread.run(Thread.java:745)  
      at org.jboss.threads.JBossThread.run(JBossThread.java:320)  
 Caused by: javax.resource.ResourceException: IJ031089: Failed to load datasource: org.postgresql.Driver  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getDataSource(LocalManagedConnectionFactory.java:652)  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:313)  
      ... 36 more  
 Caused by: java.lang.ClassCastException: org.postgresql.Driver cannot be cast to javax.sql.DataSource  
      at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getDataSource(LocalManagedConnectionFactory.java:635)  
      ... 37 more  

First of all: wrong hibernate settings in persistance.xml. I went to google and took ones for Postgres:
 <?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="postgres">  
     <!-- If you are running in a production environment, add a managed  
       data source, this example data source is just for development and testing! -->  
     <!-- The datasource is deployed as WEB-INF/greeter-quickstart-ds.xml,  
       you can find it in the source at src/main/webapp/WEB-INF/greeter-quickstart-ds.xml -->  
     <jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>  
     <!--Next is for version 9.4 and higher -->
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>  
     <properties>  
      <!--  <property name="hibernate.archive.autodetection" value="class" /> -->  
       <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL94Dialect" />  
       <!--  this I comment because I already have my connection registered as a module
       <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />  
       <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5433/cats" />  
       <property name="hibernate.connection.username" value="postgres" />  
       <property name="hibernate.connection.password" value="mei" />  
       Next I setup value of show_sql for false and hb2ddl.auto for "create-drop"-->  
       <property name="hibernate.show_sql" value="false"/>  
       <property name="hibernate.flushMode" value="FLUSH_AUTO" />  
       <property name="hibernate.hbm2ddl.auto" value="create-drop" />  
     </properties>  
   </persistence-unit>  
 </persistence>  
   

Then I need to add some new dependencies to my pom.xml:
     <dependency>  
       <groupId>org.hibernate</groupId>  
       <artifactId>hibernate-entitymanager</artifactId>  
       <version>5.2.4.Final</version>  
     </dependency>   
   
     <dependency>  
       <groupId>org.postgresql</groupId>  
       <artifactId>postgresql</artifactId>  
       <version>9.4.1211</version>  
     </dependency>  

Then I've got a BUILD SUCCESS wiht my project deployment.

Thursday, November 3, 2016

Add another datasource to your WildFly.

To create my own database and deploy in on my WildFly serve I will use PostgreSQL sql system.
I already installed Postgres Server, Pgadmin to manage your database and StackBuilder - all in one from official download page.
I installed two servers: one on the 5432 port and one on the 5433. the last one I started to use and added there new database cats, there I creater a schema list where I put the table cats.
There are two variants how to include database to wildfly server: as deployment or as module. I will add my datasource as a module.

To do it I create a folder named postgres in %WILDFLY_HOME%\modules\system\layers\base\org wherewe create a new folder main and file module.xml inside this folder. Also download jdbs postgres driver
In module.xml I configure access to our driver:
1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <module xmlns="urn:jboss:module:1.1" name="org.postgres">  
3:    <resources>  
4:      <resource-root path="postgresql-9.4.1211.jar"/>  
5:    </resources>  
6:    <dependencies>  
7:      <module name="javax.api"/>  
8:      <module name="javax.transaction.api"/>  
9:      <module name="javax.servlet.api" optional="true"/>  
10:    </dependencies>  
11:  </module>  


Then I modify standalone.xml which I can find in %WILDFLY_HOME%\standalone\configuration.
There, between the tags 
 <datasources> </datasources>  
we describe our custom datasource:
1:      <subsystem xmlns="urn:jboss:domain:datasources:4.0">  
2:        <datasources>  
3:          <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">  
4:            <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>  
5:            <driver>h2</driver>  
6:            <security>  
7:              <user-name>sa</user-name>  
8:              <password>sa</password>  
9:            </security>  
10:          </datasource>  
11:          <datasource jta="false" jndi-name="java:jboss/datasources/PostgresDS" pool-name="PostgresDS" enabled="true" use-java-context="true">  
12:            <connection-url>jdbc:postgresql://localhost:5433/cats</connection-url>  
13:            <connection-property name="cats">dictionary</connection-property>  
14:            <driver>postgres</driver>  
15:            <security>  
16:                 <user-name>postgres</user-name>  
17:                 <password>postgres</password>  
18:            </security>  
19:          </datasource>  
20:          <drivers>  
21:            <driver name="postgres" module="org.postgres">  
22:                 <datasource-class>org.postgresql.Driver</datasource-class>  
23:                 <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>  
24:            </driver>  
25:                 <driver name="h2" module="com.h2database.h2">  
26:             <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>  
27:            </driver>  
28:          </drivers>  
29:        </datasources>  
30:      </subsystem>  

Here we see both: ExampleDS and our custom PostgreDS. There I combined datasources separately from drivers declarations and those declarations I put at the end of the datasources div as you can see.
Connection URL is where our datasource is. Connection-property we need since wildfly 10.1.0, here we set up the name of our database and the name of our application.

Now if we  start our server
Open your command prompt:

      a. Push WIN ΓΏ button on your keyboard, in appeared window type "cmd", then ENTER.
     or

      b. Go "Start" -> Search -> type "cmd" -> choose "Command prompt".
then type
%WILDFLY_HOME%\bin\standalone.bat
we will find our new datasource:
Next time I will try to lookup data from this datasource to my servlett and update datasource from application.

How to post code in your blogspot?

This is a source to make your code formatted as you need