Java: JBoss - Configuración de Persistencia con JPA...
¿Cómo configurar el Data Source (MySQL) y persistencia con JBOSS (7.1.1) y JPA en un proyecto Java?
Si queremos usar persistence JPA con JBoss y MySQL podemos hacer lo siguiente para configurar el Datasource de MySQL y buestra base de datos:
1) Instalar JBoss-as-7.1.1.Final
Primero debemos tener instalados JBoss (por ejemplo jboss-as-7.1.1.Final).
2) MYSQL connector
Bajar el JAR de mysql connector e instalarlo en <DIR_de_install>\modules\com\mysql\main\:
Por ejemplo:
C:\jboss-as-7.1.1.Final\modules\com\mysql\main\mysql-connector-java-5.1.33-bin.jar
Editar C:\jboss-as-7.1.1.Final\modules\com\mysql\main\module.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
C:\jboss-as-7.1.1.Final\modules\com\mysql\main\mysql-connector-java-5.1.33-bin.jar
~ JBoss, Home of Professional Open Source.
~ Copyright 2010, Red Hat, Inc., and individual contributors
~ as indicated by the @author tags. See the copyright.txt file in the
~ distribution for a full listing of individual contributors.
~
~ This is free software; you can redistribute it and/or modify it
~ under the terms of the GNU Lesser General Public License as
~ published by the Free Software Foundation; either version 2.1 of
~ the License, or (at your option) any later version.
~
~ This software is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
~ Lesser General Public License for more details.
~
~ You should have received a copy of the GNU Lesser General Public
~ License along with this software; if not, write to the Free
~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<module xmlns="urn:jboss:module:1.0" name="com.mysql">
<resources>
<resource-root path="mysql-connector-java-5.1.33-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
<module name="javax.servlet.api" optional="true"/>
</dependencies>
</module>
3) Agregar el Datasource en JBoss
Editar standalone.xml de JBoss:
<datasources>
<datasource jndi-name="java:jboss/datasources/MysqlDS" enabled="true"
use-java-context="true" jta="true" use-ccm="true" pool-name="MysqlDS">
<connection-url>jdbc:mysql://localhost:3306/MY_DATA_BASE</connection-url>
<driver>mysql</driver>
<pool>
<max-pool-size>30</max-pool-size>
</pool>
<security>
<user-name>USER</user-name>
<password>my_sql_pass</password>
</security>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
</datasources>
4) Configurar Persistence en el proyecto
Editar o agregar: src/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MysqlDS</jta-data-source>
<!-- "java:jboss/datasources/MysqlDS" apunta al datasource del archivo STANDALONE.XML de JBOSS -->
<!-- Alli esta configurada jdbc:mysql://localhost:3306/MY_DATA_BASE -->
<class>package.domain.entity.MyTableEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
4) Configurar EntityManagerFactory en el código
...EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = entityManagerFactory.createEntityManager();
...
5) Lanzar JBoss
Si todo está bien JBoss debería loguear las líneas siguientes (en log o en consola):...
20:14:33,717 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
...
20:14:35,941 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) JBAS010400: Bound data source [java:jboss/datasources/MysqlDS]
...
20:14:36,037 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "RentalHousingWebService.war"
20:14:37,258 INFO [org.jboss.as.jpa] (MSC service thread 1-1) JBAS011401: Read persistence.xml for persistenceUnit
...
No hay comentarios:
Publicar un comentario