I'm building a web application using Spring MVC. I've been trying to come up with a way to customise some system properties and reload the web application without restarting the Tomcat server.
Currently, I'm doing this task by using refresh() method in ConfigurableApplicationContext and its working at the moment.
#Autowired
ConfigurableApplicationContext configurableApplicationContext;
..
public void reloadConfigurations() {
configurableApplicationContext.refresh();
}
However, if the user logs out after refreshing the system, then after that he will not able to login to the system again.
Here's the message I get as SPRING_SECURITY_LAST_EXCEPTION :
entitymanagerfactory is closed; nested exception is
java.lang.illegalstateexception: entitymanagerfactory is closed.
Can't see any other errors in system logs or tomcat log.
Here's the code
application-cotext.xml
<bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="mysqlDataSource" />
<property name="persistenceUnitName" value="myAppMySql" />
<property name="packagesToScan" value="com.myApp.model" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
persistence.xml
<persistence-unit name="myAppMySql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.myApp.model.SystemConfig</class>
<class>com.myApp.model.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.connection.pool_size" value="5"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="10"/>
</properties>
</persistence-unit>
Any ideas?? Thanks in advance
Related
I want keep 2 datasources for 2 different databases (both are mysql) like below :
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
</bean>
<bean id="dataSource-A"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url-A}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory-A"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource-A" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<!-- spring based scanning for entity classes -->
<property name="packagesToScan" value="com.package-A" />
</bean>
<bean id="dataSource-B"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url-B}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="entityManagerFactory-B"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource-B" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<!-- spring based scanning for entity classes -->
<property name="packagesToScan" value="com.package-B" />
</bean>
Now when i do simply this, beanInitializer gives error that entityManagerFactory is not found.
How can i have multiple entityManagerFactory, for multiple data base and what is best way to do in xml.
I googled quite a lot, but none is able to solve my problem.
It works well if i define only one entityManagerFactory, I am just stuck to have miltiple entityManagerFactory in same application context.
Below is the example for keeping multiple persistence unit for different type of database. Similarly 2 unit can be kept for both of your sql databases.
<?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="oraPersistent" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.kulhade.us.ora.entity.BilltoAddress</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>AUTO</validation-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="${db.driver}"/>
<property name="javax.persistence.jdbc.url" value="${db.url}"/>
<property name="javax.persistence.jdbc.user" value="${db.username}"/>
<property name="javax.persistence.jdbc.password" value="${db.password}"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="hibernate.jdbc.batch_size" value="50"/>
</properties>
</persistence-unit>
<persistence-unit name="mongoPersistent" transaction-type="JTA">
<!-- Use Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>com.kulhade.us.mongo.entity.Sample</class>
<class>com.kulhade.us.mongo.entity.SampleLine</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<property name="hibernate.ogm.datastore.database" value="${mongodb.name}"/>
<property name="hibernate.ogm.mongodb.host" value="${mongodb.host}"/>
<property name="hibernate.ogm.datastore.port" value="${mongodb.port}"/>
<!--<property name="hibernate.ogm.datastore.document.association_storage" value="ASSOCIATION_DOCUMENT"/>
<property name="hibernate.ogm.mongodb.association_document_storage" value="COLLECTION_PER_ASSOCIATION"/>-->
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
These multiple persistence unit can be used in Spring orm. Below is the example for it.
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>org/springframework/orm/jpa/domain/persistence-multi.xml</value>
<value>classpath:/my/package/**/custom-persistence.xml</value>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="local-db"/>
<entry key="remoteDataSource" value-ref="remote-db"/>
</map>
</property>
<!-- if no datasource is specified, use this one -->
<property name="defaultDataSource" ref="remoteDataSource"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="myCustomUnit"/>
</bean>
I am developing a web application and I need two DataSource to connect two difference database according to my requirement.One DataSource will use Spring + JPA framework and another DataSource is use Spring + MyBatis framework.
Yes you can I suggest manage both from spring and obtained from the applicationContext.
<bean class="org.apache.tomcat.jdbc.pool.DataSource" id="dataSource" >
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<bean class="org.apache.tomcat.jdbc.pool.DataSource" id="dataSourceOrderDetail" >
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url.orderdetail}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
Just need to have different name of id, to be properly injected
Check this to review how you can integrate spring with ibatis then configure beans using the datasource beans
Or if you want to use datasource-ds.xml just put the two datasource xml files in the lib folder within your application context, if you are using something like jboss or tomcat.
UPDATE
<jpa:repositories base-package="com.staples.sa.pricemart.repository.pag"
entity-manager-factory-ref="entityManagerFactory" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<qualifier value="pagTransactionManager" />
</bean>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<jpa:repositories base-package="com.staples.sa.pricemart.repository.orderdetail"
entity-manager-factory-ref="entityManagerFactoryOrderDetail" />
<bean id="transactionManagerOrderDetail" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryOrderDetail" />
<qualifier value="orderDetailTX" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactoryOrderDetail">
<property name="persistenceUnitName" value="persistenceUnitOrderDetail" />
<property name="dataSource" ref="dataSourceOrderDetail" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<!-- -->
Persistence.xml need to look like this. (need to complete the xml config)
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
And
<!-- Add the persistence context for OrderDetail -->
<persistence-unit name="persistenceUnitOrderDetail"
transaction-type="RESOURCE_LOCAL">
Here is the sample code for you
class Main {
public static void main(String args[]) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");
Context.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#oracle.devcake.co.uk:1521:INTL"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://dbhost-prospring-psql/prospring"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
You can configure as many databases as you like in Spring context by declaring datasource beans with different id's and
injecting appropriate properties from properties file. if the two databases are different then you are in territory of distributed transactions and you must configure a Spring transaction manager which can operate with JTA. Also worth noting is that Spring transaction manager is merely an abstraction and it needs to have external JTA transaction
manager configured (like Bitrionix / Atomikos) or if deploying under EE application server then transaction manager can be looked up in JNDI registry. Then when you mark the transaction boundary (perhaps using Spring Transactional annotation) then Spring would automatically co-ordinate the transactions.
I using Mule Studio version: 1.3.1 .
buildDate: 201209061215
I am not able to get JPA end point. I also downloaded jpa-connector-1.0-20120925-2201.jar
But I dont know to to integrate with mule studio.
So I decided to use a simple Java transformer and write my processing logic which will be internally using JPA/Hibernate.
I came to know that i have to use a JPA vendor adapter for spring, else none of my service, DAO classes will be instantiated.
I have declared a datasource and entityManager as beans of spring inside mule flow xml as shown.
<spring:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<spring:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<spring:property name="url" value="jdbc:mysql://localhost:3306/eigDB" />
<spring:property name="username" value="root" />
<spring:property name="password" value="tiger" />
</spring:bean>
<spring:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<spring:property name="dataSource" ref="dataSource" />
<spring:property name="persistenceUnitName" value="autoRebateSystem" />
<spring:property name="jpaVendorAdapter">
<spring:bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<spring:property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
<spring:property name="showSql" value="true" />
</spring:bean>
But mule flow xml is not able to recognize any class of spring framework.
Finding very difficult to replace those class names.
Plez provide the solution for my problem,
By letting me know the PROPER replacements for mule studio.
1) org.springframework.jdbc.datasource.DriverManagerDataSource
2) org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
3) org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
Are you missing to use HibernateJpaDialect and JpaTransactionManager;
I hope the following configuration will be useful.
<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/yourdatabase"/>
<property name="username" value="your- username"/>
<property name="password" value="your- password"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--<property name="dataSource" ref="dataSource"/>-->
<property name="persistenceUnitName" value="<your-persistunit-name>"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect/>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
</bean>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<!-- <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />-->
<property name="generateDdl" value="false"/>
<property name="showSql" value="false"/>
</bean>
To integrate it within MuleStudio, please add this update site within MuleStudio:
http://tecnologia.2020mobile.es/jpa-cloud-connector/update-site/
#Service
#Repository
#Transactional
public class VideoService {
#PersistenceContext
EntityManager entityManager;
public void save(Video video) {
Video video1 = new Video();
entityManager.persist(video1);
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="video_pu" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/video" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="video_pu"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- post-processors for all standard config annotations -->
<context:annotation-config/>
The transaction in service method save(Video video) is never started so also never commited. Where is the error? When I use EntityManagerFactory it works perfectly, but I don't want to explicitly begin and commit transaction. I want to use it with #Transactional annotation.
#beerbajay is correct, #Transactional will need a dynamic proxy to be created on your bean to apply the transactional logic, which can be created if your Service has an interface, since in your case it doesn't an alternate would be to instruct Spring to create class based proxy, the following way:
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class='true/>
i am trying to migrate to hibernate 4.1.0.Final with spring 3.1.1.RELEASE
and following is my configuration for hibernate:
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="${project.groupId}.domain" />
<!-- control the behavior of Hibernate at runtime,All are optional and
have reasonable default values -->
<property name="hibernateProperties">
<value>
<!-- hibernate.dialect: allows Hibernate to generate SQL optimized for
a particular relational database -->
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=create-drop
hibernate.show_sql=false
hibernate.jdbc.fetch_size=100
hibernate.jdbc.batch_size=100
hibernate.jdbc.batch_versioned_data=true
hibernate.order_inserts=true
hibernate.order_updates=true
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
</value>
</property>
</bean>
<!-- provides properties to hibernate to make it able to create session
factory. Hibernate uses instance of session bean of type -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- responsible for creating sessionFactory opening transactions and binding
them to the current thread context. -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="nestedTransactionAllowed" value="true" />
</bean>
<!-- get exception translation from HibernateException into DataAccessException
hierarchy -->
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
but when trying to run the application, i got the following exception:
java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
please advise why i get this error, and how to fix it, thanks.
Try using the org.springframework.orm.hibernate4.HibernateTransactionManager
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>