What is the difference between using datasource and using hibernateProperties. I want to use c3P0 with spring in my app. I found 2 ways to do so but I'm unable to understand the difference between the two
First:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
depends-on="dataSource">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="maxPoolSize" value="10" />
<property name="numHelperThreads" value="5" />
</bean>
Second:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
>
<property name="hibernateProperties">
<props>
<property name="hibernate.c3p0.maxSize" value="100" />
<property name="hibernate.c3p0.numHelperThreads" value="5" />>
</props>
</property>
</bean>
The first you get a Spring managed datasource, which you can also use for a JdbcTemplate or other work.
The second you get a hibernate managed datasource which is not reusable by Spring.
I strongly suggest the first approach as it also makes it quite easy to replace your datasource for testing (by replacing it with an in-memory database) or to replace it with a JNDI lookup.
Related
I'm building manager web project with Spring MVC , Hibernate and MySQL. When i write the code to list all the regency i have in database. It run very fast in couple first times, but when i reload the page repeatedly (8 - 10 times), my page just keep the loading mode and no sign of stopping. First, i use MSSQL Server then i change to MySQL, nothing changed, i turn off the firewall, still not working.
This my dispatcher-servlet.xml:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" cache- period="31556926"/>
<mvc:annotation-driven />
<context:component-scan base-package="com.hinet.storage" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass=" org.springframework.web.servlet.view.tiles3.TilesView"/>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"
p:definitions="/WEB-INF/tiles/tiles.xml"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/database.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.hinet.storage" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="configurationClass">org.hibernate.cfg.AnnotationConfiguration</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
And this is RengencyDAOImpl:
#Repository("regencyDAO")
#Transactional
public class RegencyDAOImpl extends BaseDAOImpl implements RegencyDAO{
#Override
public List<Regency> getRegencies() {
String sql = "from Regency as re where re.isDelete != true";
Query query = this.openSession().createQuery(sql, Regency.class);
List<Regency> regencies = query.list();
return regencies;
}
}
It has given me headache in the past few days! Does anyone know the
reason that cause this problem?
I think you exhaust your datasource (i.e opening without closing connection).
What does openSession() do? I think it should be something like getCurrentSession().
If you open new session, you should close it.
i am a newbie to spring and hibernate..Here in my scenario, i am building an application where it can connect to different environments Db's(devl,qual,prod) based on the environment selected by the user..For this I am making multiple SessionFactory beans and distinguishing them using #Qualifier in DAO classes based on the env selected by the user..
Here i am struck at, how to configure multi-TransactionManagers if they are required or not in my case and how to deal with the code if the multi-TransactionManagers are not required..?
So I was not sure of how to use different TransactionManagers per env, for the same save method which will save the user form data in the DB..Below is my spring .xml file and the DAO class code which is used to distinguish the sessionFactory w.r.t env..
Help needed in configuring multiple TransactionManagers or can I discard the use of TransactionManager and simply distinguish the transaction with Hibernate session(beginTransaction)..!!
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceProd" />
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSourceProd" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=SM_PROD" />
<property name="username" value="sa" />
<property name="password" value="123" />
</bean>
<!-- Hibernate Properties -->
<bean id="prodSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceProd" />
<property name="packagesToScan" value="com.deere.dsfj.siteMinder.domain" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="dataSourceDevl" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=SM_Devl" />
<property name="username" value="sa" />
<property name="password" value="123" />
</bean>
<bean id="DevlSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceDevl" />
<property name="packagesToScan" value="com.deere.dsfj.siteMinder.domain" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Below is the DAO code..
public void BeginTransaction(String targetEnv)
{
getCurrentSession(targetEnv).getTransaction().begin();
}
#Autowired
#Qualifier("DevlSessionFactory")
private SessionFactory devlSessionFactory;
#Autowired
#Qualifier("prodSessionFactory")
private SessionFactory prodSessionFactory;
protected final Session getCurrentSession(String targetEnv)
{
if(targetEnv!=null && !"".equals(targetEnv) && targetEnv.equals("Devl"))
{
return devlSessionFactory.getCurrentSession();
}
else if(targetEnv!=null && !"".equals(targetEnv) && targetEnv.equals("PROD"))
{
return prodSessionFactory.getCurrentSession();
}
return null;
}
Appreciate the help...
I want to make a test with HQL but the mapping tables is with Annotations.
Hibernate configuration file is in the /WEB-INF/spring-config-ws.xml :::>
<!-- Activate transaction declarations with annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Property files application uses -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- JNDI DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${dataSource.jndiName}" />
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="packagesToScan">
<list>
<value>es.sergas.rprof.profesional.domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
</props>
</property>
</bean>
<!-- Transaction manager for Hibernate SessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
When I run a list, I get that you do not find the hibernate cfg:
hibernate.cfg.xml not found
I just want to list a mapped class with annotations, but HQL
I feel my level of English is so low. Thank you
Make sure your hibernate.cfg.xml is inside src/main/resources, if the file is not there you need to specify the right location, so put it inside this folder that your problem will be solved.
Note that we don’t have to explicitly mention the mapping or configuration or properties
files, because the Hibernate runtime looks for default filenames, such as hibernate.
cfg.xml or hibernate.properties, in the classpath and loads them. If we have a nondefault
name, make sure you pass that as an argument—like configure("my-hibcfg.
xml"), for example.
I have successfully configured hibernate and I can run transactions but only from the psvm of the DAO class. I want to configure it with my spring app using the same configuration file i.e. hibernate.cfg.xml.
How can I do this? Most tutorials I've read simply neglect the hibernate configuration file.
You can add this code to you xml file to configure hibernate.
<!-- Hibernate Related Configuration. -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://192.168.1.9:5432/dbname"/>
<property name="username" value="postgres"/>
<property name="password" value="pwd"/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.domain"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
The hibernate.cfg.xml file is specified for the LocalEntityManagerFactoryBean, along with your DataSource
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/hibernate.cfg.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
Here you can find an example of a Spring XML configuration containing some Hibernate configuration
I want to rewrite my sessionFactory bean using the p-namespace. I understand how to reference general objects, but I've never dealt with lists, props, etc. How would I go about writing , and so on?
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>/com/mysite/domain/Object.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
This is the code I have so far in p-namespace:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingResources-list="/com/mysite/domain/Object.hbm.xml"
p:hibernateProperties-props=""/>
You cannot do this directly . You could use util:map though. Try
<util:map id="hibernateConfig" >
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.show_sql" value="true" />
<!-- Other properties -->
</util:map>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingResources-list="/com/mysite/domain/Object.hbm.xml"
p:hibernateProperties-ref ="hibernateConfig"/>
Check This for complete help on p:namespace