I am learning JPA.
I tried the Spring-JPA-Hibernate combination.
The application works and I get the results.
But though there is only 1 record in the database, it is taking about 4 to 5 minutes to load the page.
Not quite sure what I am doing wrong.
I Googled for the same but all I am getting are performance optimization suggestions but I think I am missing something more basic as there is only 1 record in my table.
Here are the details:
Persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="bike_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/testdb123</jta-data-source>
<class>bike.Bike</class>
<class>bike.Company</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bike"/>
<property name="javax.persistence.jdbc.user" value="abc"/>
<property name="javax.persistence.jdbc.password" value="abc"/>
</properties>
</persistence-unit>
</persistence>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>bikeDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bikeDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/home.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<resource-env-ref>
<resource-env-ref-name>jdbc/testdb123</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
</web-app>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/testdb123" auth="Container"
type="javax.sql.DataSource"
username="abc" password="abc"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost:1527/bike" />
</Context>
I am using Spring 3 and Hibernate 3 jar files.
I tried replacing Hibernate with Eclipselink and the applications works perfect. Its only when I use JPA with Hibernate that I have slowness issues.
Here are the jars I used with Eclipselink and Hibernate:
Eclipselink:
eclipselink.jar
javax.persistence_2.1.0.v201304241213.jar
javax.persistence.source_2.1.0.v201304241213.jar
Hibernate:
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
With Eclipselink, this is how my persistance.xml looks:
<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="bike_unit" transaction-type="RESOURCE_LOCAL">
<class>bike.Bike</class>
<class>bike.Company</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bike" />
<property name="javax.persistence.jdbc.user" value="abc" />
<property name="javax.persistence.jdbc.password" value="abc" />
</properties>
</persistence-unit>
</persistence>
Not sure what I am missing.
I had this performance problem on Windows 7 Desktop.
However, I did not face this issue when tried on a Windows 8 Machine with identical code and database.
Did not find the root cause but solved my problem for now.
Related
I am developing j2EE application it is web sotre I use Jboss7 , maven and wildfly my problem is that after every login database content will be deleted .. any suggestion to solve this ?
this is my persistance file
<?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="PiDevECommerce-ejb">
<jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>
<class>ecommerce.entites.Admin</class>
<class>ecommerce.entites.Brand</class>
<class>ecommerce.entites.Category</class>
<class>ecommerce.entites.Coupon</class>
<class>ecommerce.entites.Customer</class>
<class>ecommerce.entites.Event</class>
<class>ecommerce.entites.Offre</class>
<class>ecommerce.entites.Product</class>
<class>ecommerce.entites.Publication</class>
<class>ecommerce.entites.ReclamationCoupon</class>
<class>ecommerce.entites.ReclamationOffre</class>
<class>ecommerce.entites.Review</class>
<class>ecommerce.entites.Store</class>
<class>ecommerce.entites.Storemanager</class>
<class>ecommerce.entites.Transaction</class>
<class>ecommerce.entites.TransactionPK</class>
<class>ecommerce.entites.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
The problem is in your configuration
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
The correct value would be validate
This post answer your question completely
Click here
I am working on a project using Spring 4 MVC, AngularJS, and Websphere 7. When I build my application as a war and install it through the Websphere 7 admin console everything works fine. However, I need my war wrapped in an EAR (not the generic EAR that websphere automatically wraps it in). So I created a seperate project that takes the WAR and wraps it in an EAR.
When I go to the url http://localhost:9080/public/myapp then I get a 404 that it can't find the resources. When I add a trailing slash on it like this http://localhost:9080/public/myapp/ then it redirects to the index.html resource just fine. I need it to find the index.html when I go to the url without the trailing slash. How can I get that to work?
Here's my configuration files:
application.xml in the EAR
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" version="5">
<display-name>myapp-ear</display-name>
<module>
<web>
<web-uri>myapp-web-2.0-SNAPSHOT.war</web-uri>
<context-root>/public/myapp</context-root>
</web>
</module>
</application>
ibm-web-bnd.xml in the WAR
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
version="1.0">
<virtual-host name="default_host"/>
<resource-ref name="UwProfileDataSource" binding-name="jdbc/uwprofile"></resource-ref>
</web-bnd>
web.xml in the WAR
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<resource-ref>
<description>myapp Service</description>
<res-ref-name>UwProfileDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
myapp-servlet.xml in the WAR
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.myapp"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<mvc:resources mapping="/**" location="classpath:/app/"/>
<mvc:view-controller path="/" view-name="forward:/index.html" />
<bean id="uwProfileDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>UwProfileDataSource</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>
</beans>
In my webapp in Eclipse, my xml configuration files are never loaded when I launch the web project.
Can you explain me how I can add the xml files in src/main/webapp/WEB-INF to the context loader please ?
My 3 files are :
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>
index.jsp
index.html
</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml,
/WEB-INF/cxf-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
cxf-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- these are included in the dependency jar -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- soap container -->
<jaxws:endpoint
id="computerWS"
implementor="#computerService"
address="/computer"
/>
</beans>
application-context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<import resource="classpath:serviceContext.xml" />
<import resource="classpath:bindingContext.xml" />
<context:component-scan base-package="com.excilys.formation.webservice.service" />
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
</beans>
Thanks
Make sure that tomcat deploys your application. If you are not getting any error during the deployment based on your application, it is probably tomcat issue.
I know there are a lost postings with the same upcoming exception. But I tried them all and tried finally 3 different tutorials. And even these example sources are running into the following error:
Caused by: java.lang.IllegalArgumentException: A universal match pattern ('/**') is defined before other patterns in the filter chain, causing them to be ignored. Please check the ordering in your <security:http> namespace or FilterChainProxy bean configuration
at org.springframework.security.config.http.DefaultFilterChainValidator.checkPathOrder(DefaultFilterChainValidator.java:49) [spring-security-config-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.security.config.http.DefaultFilterChainValidator.validate(DefaultFilterChainValidator.java:39) [spring-security-config-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy.afterPropertiesSet(FilterChainProxy.java:151) [spring-security-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) [spring-beans-3.1.4.RELEASE.jar:3.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) [spring-beans-3.1.4.RELEASE.jar:3.1.4.RELEASE]
... 21 more
Am I doing something crucial wrong? Any advises would be appreciated. As I'd like to secure REST-methods a way with JavaConf would be good or even better as well =)
My web.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Backend</display-name>
<!-- RESTEasy configuration -->
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<!-- RESTEasy <-> Spring connector (RESTEasy can access Spring beans) -->
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Custom name for main spring configuration -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
My spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.tripple" />
<mvc:annotation-driven />
<!-- Needed for Autowiring -->
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- MySQL DataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/tripple" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.tripple.entity" />
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<security:http create-session="stateless"
authentication-manager-ref="authenticationManager">
<security:intercept-url pattern="/**" access="ROLE_USER" />
</security:http>
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider ref="customAuthenticationProvider" />
</security:authentication-manager>
And finally my dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<tx:annotation-driven />
<mvc:annotation-driven />
Looking towords your configuration it seams that your Spring context loading twice
go throw following links
Spring MVC web app: application context starts twice
Spring context loading twice with both xml and annotation configuration
Application context loading twice
another problem might be a dependency conflict problem.
check for dependency conflicts ,cleaning up dependency conflicts and try to start up the application
hope this will help you !
The problem was, that I had a old JavaConf Class in the project as well. Therefore the context was loaded twice.
Removed the class and everything worked like a charm =)
I've created a spring project which I later deployed on glassfish, the ap is working at first. But when I added persistence.xml it took mins to be done with error.
<?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="sidoPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDatasource</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.JTATransactionFactory" />
</properties>
</persistence-unit>
</persistence>
When browse: http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd, it wouldn't load.
Any idea or work around on this issue?
I think it's related with the whole java.net, java.sun.com, project Kenai etc. being down because of the maintenance reasons.
https://blogs.oracle.com/theaquarium/entry/outage_on_java_net_apr