Hibernate not retrieving data with JBOSS - java

I am trying to get the following configured.
Spring, Hibernate, Jboss 5.1, Mysql 5.14
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring- mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring- tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="au.com.kiche" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- Most controllers will use the ControllerClassNameHandlerMapping above,
but for the index controller we are using ParameterizableViewController,
so we must define an explicit mapping for it. -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<!-- the following bean description should be moved to applicationContext. I'm leaving this decision to Chief. -->
<jee:jndi-lookup id="dataSource" jndi-name="java:/MyDS"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
hibernate.cfg.xml is:
<?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="hibernate.connection.datasource">java:/MyDS</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping class="au.com.kiche.model.User"/>
</session-factory>
</hibernate-configuration>
The data source file "kiche-ds.xml" is:
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/kiche</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root5</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>100</max-pool-size>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
The followis is my class that is try to get all users from the DB:
#Repository
public class UserDAOImpl implements UserDAO{
#Autowired
SessionFactory sessionFactory;
#Override
public List<User> getUserByLogin() {
Criteria criteria=sessionFactory.getCurrentSession().createCriteria(User.class);
List<User> users=criteria.list();
System.out.println("**** The size of the user is: "+users.size());
return users;
}
.
.
.
}
The strange part is that I am able to get the data form the DB when I use Tomcat(not the one with JBOSS). But when I try to run this application in JBOSS, NO data is retrived, there are no errors or exceptions.
Any help will be highly appraciated.
Regards
Adofo

You should use the Java EE convention of resource mapping. For your case:
application context:
<jee:jndi-lookup id="dataSource"
jndi-name="MyDS"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"
resource-ref="true"
/>
WEB-INF/web.xml:
<resource-ref>
<res-ref-name>MyDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
WEB-INF/jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC
"-//JBoss//DTD Web Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
<jboss-web>
<resource-ref>
<res-ref-name>MyDS</res-ref-name>
<jndi-name>java:MyDS</jndi-name>
</resource-ref>
</jboss-web>
This will work for both JBoss and pure Tomcat.

Related

Is c3p0 configurable on the server instead of WAR file?

Iam learning how to configure hibernate with tomcat. Now
i want to configure connection pooling on tomcat.
I want to configure connection pooling on the server to share the pool with two applications. At the moment i have a java app including all my jar files.
-Mysql-connector-
-hibernate-
-c3p0-
Tomcat has a build in pool that connection can be obtained with JNDI.
Now how can i use c3p0 to share my pool with multiple apps?
Do i have to use the build in pool? i dont want to deploy multple pools with my WAR files.
You can use whatever connection pooling you wish with Tomcat and JNDI. You specify the pool with the type property of the Resource tag. If you want this to be accessible to multiple web apps, then the resource needs to be specified in server.xml
For example
<Resource name="jdbc/myDataSource"
auth="Container"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
driverClassName="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://localhost/myDataSource"
user="user" password="password"
minPoolSize="3"
maxPoolSize="15"
maxIdleTime="5000"
idleConnectionTestPeriod="300"
acquireIncrement="3" />
you can use this config in your apps (~.cfg.xml) to check the DataSource by JNDI
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dataSource">java:comp/env/jdbc/JNDI_Name</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- your dialect-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="mapping/xxx.hbm.xml"/>
<mapping resource="mapping/yyy.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If you use Spring xml conf, create an xml file for Datasource && sessionFactory && transactionManager:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<context:component-scan base-package="x.y.z.*" />
<context:annotation-config />
<import resource="classpath:~/~.cfg.xml" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/Your_JNDI_Name"/>
</bean>
<!-- OR ORM HIBERNATE PART -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:~/~.cfg.xml</value>
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<!-- Declaration of DOA beans Hibernate -->
<bean id="myDao" class="class_dao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
I hope that's helpful for you

NullPointer exception on managed property (service)

I am facing a null pointer exception on my app , I am annotating Dao with #Repository , the servive by #Service , controller with #Controller and service inside it with #ManagedProperty, I am suspecting my application context is not well configured so here there is:
<?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:context="http://www.springframework.org/schema/context"
xml:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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.2.xsd">
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config/>
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.domain.nameOfapp.*" />
<!-- Necessary to get the entity manager injected into the factory bean -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Define Hibernate JPA Vendor Adapter -->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jpa-persistence" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan">
<list>
<value>com.domain.nameOfapp.*</value>
</list>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Detect #Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
Any help would be great! thanks
Your component scan needs to be as below,
<context:component-scan base-package="com.domain.nameOfapp" />
This will scan all the classes under this package including any sub-packages.
Also using #ManagedProperty, are you trying to autowire the spring service bean? I believe you should be using #Autowired.

Spring configuration and transaction

I played with this numerous times, changing the default target namespace, still it will complain the context undeclared element: Multiple annotations found at this line: - cvc-elt.1: Cannot find the declaration of element 'context:annotation-driven'.
------Configuration File----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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-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.2.xsd">
<context:annotation-driven/>
<tx:annotation-driven/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/classes/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
...
Also, I added two annotation-driven since the transaction is not working, does tx:annotation-driven use the transactionManager which has it's own session from hibernate?
I used my derived sessionFactory using hibernate3, so only need a transactional before the method to run update queries.
Thanks!
Try to add <?xml version="1.0" encoding="UTF-8"?> and better use java-based config

PropertyPlaceholderConfigurer values are not getting resolved for beans in imported context

I have PropertyPlaceholderConfigurer configured in my web application's Spring context which in turn imports few other contexts which are in JARs that expect certain properties to be configured. But for some reason the PropertyPlaceholderConfigurer values ware not available to them and I get error on start up:
java.net.URISyntaxException: Illegalcharacter in path at index 1: ${dax.svc1.endpoint}
Here is what my application context looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 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-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" name="mhpVariables">
<property name="locations">
<list>
<value>classpath:appconfig.properties</value>
</list>
</property>
</bean>
<import resource="classpath:com.test.svc1/childContext.xml"/>
<import resource="classpath:com.test.svc2/child2Context.xml"/>
</beans>
Child context is like this :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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-2.0.xsd">
<!-- connection info -->
<bean class="com.test.java.framework.dataaccess.ServiceConnectionInfo" id="ConnectionInfo">
<property name="defaultUri" value="${dax.svc1.endpoint}"/>
<property name="maxTotalConnections" value="500"/>
<property name="maxConnectionsPerHost" value="50"/>
<property name="readTimeout" value="3000"/>
<property name="ConnectionTimeout" value="1000"/>
</bean>
</beans>
I verified the property file is on the classpath and has the property dax.svc1.endpoint. What am I missing here?
You have to put a placeholder bean inside each of the imports; that's the only way I could get it to work as I have a similar setup to what you describe. I also removed the id from the bean to prevent any id conflicts in the container.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/myconfig.properties" />
</bean>
I will assume that you have all the xml directives... check the encoding of your properties file (also your XML)

create two connectors to DB using hibernate

I have an application based on Spring and hibernate.
My task is to create two connectors in this application to database - one connector is only to READ and second is to READ, WRITE etc.
How my configuration should looks like.
Now in WEB-INF folder i have 3 files:
hibernate-context:
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:property-placeholder location="/WEB-INF/spring.properties" />
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="${hibernate.config}"
p:packagesToScan="com.esb.scs"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
</beans>
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
spring.properties:
# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/database
app.jdbc.username=user
app.jdbc.password=password
#hibernate properties
hibernate.config=/WEB-INF/hibernate.cfg.xml
how can i create two connections to database in one application?
My question is becouse i have two databases with replication, where one is only for read and the secound is for write...
With your help i've created file like this:
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:property-placeholder location="/WEB-INF/spring.properties" />
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManagerr" />
<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="${hibernate.config}"
p:packagesToScan="com.esb.scs"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="sessionFactoryr" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSourcer"
p:configLocation="${hibernate.config}"
p:packagesToScan="com.esb.scs"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSourcer" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${appr.jdbc.username}"
p:password="${appr.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManagerr" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactoryr" />
</beans>
and now when i have in service :
#Resource(name="sessionFactory")
private SessionFactory sessionFactory;
#Resource(name="sessionFactoryr")
private SessionFactory sessionFactoryr;
and when i trying to make query with sessionFactoryr
i get Error:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
but i have
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
in web.xml
To set up two transaction managers, just declare them in your app context, I use a properties file and read in connection details. I set what type of access hibernate has in that prop file (and also restrict permissions on the db server) :
<bean id="sessionFactoryOne"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="WEB-INF/classes/hibernate.cfg.xml"
p:packagesToScan="com.mycompany" />
<bean id="dataSourceONE" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}"
p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="10"
p:maxStatements="50" p:minPoolSize="10" />
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
<bean id="sessionFactoryTWO" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSourceTWO"
p:configLocation="WEB-INF/classes/hibernateTWO.cfg.xml"
p:packagesToScan="com.mycompany"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSourceTWO" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${two.jdbc.driverClassName}"
p:jdbcUrl="${two.jdbc.url}"
p:user="${two.jdbc.username}"
p:password="${two.jdbc.password}"
p:acquireIncrement="2"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="5"
p:maxStatements="50"
p:minPoolSize="1" />
<!-- Declare a second transaction manager-->
<bean id="transactionManagerTWO" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactoryTWO">
<qualifier value="Traveller"/>
and then you can reference them like in yr service layer like so :
#Resource(name = "sessionFactoryTWO")
private SessionFactory sessionFactoryTWO;
#Resource(name = "sessionFactory")
private SessionFactory sessionFactory;
then for methods in your service layer you will have to make methods transactional :
#Transactional(readOnly = true)
public void myReadOnlyMethod(Whatever whatever)
#Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void myWriteMethod(Whatever whatever)

Categories

Resources