-
-
Save elw00d/10385011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Context antiJARLocking="true" path="/"> | |
<Resource | |
name="jdbc/MyApp" | |
auth="Container" | |
type="javax.sql.DataSource" | |
username="your_username" | |
password="your_password" | |
driverClassName="com.mysql.jdbc.Driver" | |
url="jdbc:mysql://localhost:3306/db_name" | |
maxActive="8" | |
maxIdle="4"/> | |
</Context> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='utf-8'?> | |
<!DOCTYPE hibernate-configuration PUBLIC | |
"-//Hibernate/Hibernate Configuration DTD//EN" | |
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> | |
<hibernate-configuration> | |
<session-factory> | |
<property name="show_sql">true</property> | |
<property name="format_sql">true</property> | |
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> | |
<property name="current_session_context_class">thread</property> | |
<property name="hbm2ddl.auto">update</property> | |
<property name="hibernate.max_fetch_depth">3</property> | |
<property name="connection.datasource">java:comp/env/jdbc/MyApp</property> | |
<!-- Mapping files --> | |
<mapping class="com.playground.myapp.model.User"/> | |
</session-factory> | |
</hibernate-configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.playground.myapp.web; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
import org.hibernate.service.ServiceRegistry; | |
import org.hibernate.service.ServiceRegistryBuilder; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
public class HibernateListener implements ServletContextListener { | |
private SessionFactory sessionFactory; | |
private static ServiceRegistry serviceRegistry; | |
public void contextInitialized(ServletContextEvent sce){ | |
ServletContext context = sce.getServletContext(); | |
try{ | |
Configuration configuration = new Configuration(); | |
configuration.configure(); | |
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); | |
sessionFactory = configuration.buildSessionFactory(serviceRegistry); | |
}catch (Throwable ex) { | |
System.err.println("Failed to create sessionFactory object." + ex); | |
throw new ExceptionInInitializerError(ex); | |
} | |
context.setAttribute("sessionFactory", sessionFactory); | |
} | |
public void contextDestroyed(ServletContextEvent sce) { | |
sessionFactory.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> | |
<persistence-unit name="com.playground.myapp.jpa" transaction-type="RESOURCE_LOCAL"> | |
<provider>org.hibernate.ejb.HibernatePersistence</provider> | |
<non-jta-data-source>java:comp/env/jdbc/MyApp</non-jta-data-source> | |
<!-- Classes --> | |
<class>com.playground.myapp.model.User</class> | |
<properties> | |
<property name="hibernate.connection.datasource" value="java:comp/env/jdbc/MyApp"/> | |
<property name="hibernate.id.new_generator_mappings" value ="true"/> | |
<property name="hibernate.archive.autodetection" value="class"/> | |
<property name="hibernate.show_sql" value="true"/> | |
<property name="hibernate.format_sql" value="true"/> | |
<property name="hibernate.hbm2ddl.auto" value="update"/> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> | |
<property name="connection.autocommit" value="false"/> | |
</properties> | |
</persistence-unit> | |
</persistence> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.playground.myapp.web; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
import org.hibernate.service.ServiceRegistry; | |
import org.hibernate.service.ServiceRegistryBuilder; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
public class PersistenceListener implements ServletContextListener { | |
private EntityManagerFactory entityManagerFactory; | |
public void contextInitialized(ServletContextEvent sce){ | |
ServletContext context = sce.getServletContext(); | |
entityManagerFactory = Persistence.createEntityManagerFactory("com.playground.myapp.jpa"); | |
} | |
public void contextDestroyed(ServletContextEvent sce) { | |
entityManagerFactory.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<listener> | |
<description>HibernateListener</description> | |
<listener-class>com.playground.myapp.web.HibernateListener</listener-class> | |
</listener> | |
<resource-ref> | |
<description>DB Connection</description> | |
<res-ref-name>jdbc/MyApp</res-ref-name> | |
<res-type>javax.sql.DataSource</res-type> | |
<res-auth>Container</res-auth> | |
</resource-ref> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment