Using JDBC in Spring application - spring

I am a newbie and am rather lost, would really appreciate some help.
I have a spring application which is currently setup for using JPA - using EntityManager. So the configuration files have something like
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
....
Spring injection is used to configure this, so I have a DaoImpl.java file which has
#PersistenceContext
protected EntityManager entityManager;
and EntityManager can be directly used in this java file.
However, I need to execute some native JDBC queries - EntityManager won't work. So I also need some kind of injection process here. My configuration files have
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource" primary="true">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:#${database.host}:${database.port}:${database.instance}" />
.....
But how can I use some injection process to make jdbc calls please? Can I do so without using jdbcTemplate?
I have already tried to do get an SQL connection from the EntityManager, but I got errors as
Caused by: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
I am trying to execute query
BEGIN ret := f_test(); END;
Would really appreciate some help...

Your entityManagerFactory bean needs additional configuration.
For example:
<jpa:repositories base-package="org.springbyexample.orm.repository" />
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:/schema.sql" encoding="UTF-8" />
</jdbc:embedded-database>
<!-- This is how your EM should look like -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:persistenceUnitName="hsql">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
You can check it on spring documentation:
http://www.springbyexample.org/examples/spring-data-jpa.html

Related

Transaction not getting committed

Working on a Spring 5 and Hibernate Project. Using Spring for bean management, transaction and MVC. The changes are not getting committed to database though i can see the insert statement in the log. There is no error. There is no issue with select statements. I could login into the application. Below are my configurations:
framework.xml:
<context:component-scan base-package="com.test" >
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="jtaTransactionManager" ref="transactionManager" />
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="false" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.J2eeUserTransaction">
<property name="transactionTimeout" value="300" />
</bean>
<!--
<bean id="HibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" >
<property name="transactionManager"><ref bean="atomikosTransactionManager" /></property>
<property name="userTransaction"><ref bean="atomikosUserTransaction" /></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
In the above configuration, i excluded the controllers as they are loaded using a different mvc related config files.
The service classes were annotated with #Transactional.
I tried without JTA with plain HibernateTransactionManager also. The transactions are not getting committed.
The below entry is also for hibernate to use JTA as transaction manager
hibernate.transaction.jta.platform to org.hibernate.engine.transaction.jta.
platform.internal.AtomikosJtaPlatform and
hibernate.transaction.coordinator_class to jta
I am using getCurrentSession for getting hibernate session.
I have to use Atomikos as JTA transaction manager as the development has to be happen in servlet container.
Thanks in advance for any help to find the gap in the configuration or any other issues..
The problem was with the maven Jetty plugin and the db connection created didn't persist the changes with hibernate 5 with maven jetty plugin 9.4.35.v20201120 though it was working with hibernate 3 and maven jetty plugin 7.0.1.v20091125. When I deployed the same war file with pre-configured data source in tomcat, able to persist the changes.

How can I obtain the DataSource connection through the JNDI name in Spring?

I am pretty new in Spring and I have the following problem.
I am working on an application, running into JBoss server, that have to perform queries on a DB using JdbcTemplate.
Into JBoss I have setted the JNDI name that identify the database connection.
I tryied to implement this JdbcTemplate simple example and it is pretty clear for me how JdbcTemplate works. In this example to create the connection to the database it define a dataSource bean and then inject it into the class that use the JdbcTemplate, in this way:
<!-- Initialization for data source -->
<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/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="studentJDBCTemplate"
class="com.tutorialspoint.StudentJDBCTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
So, as you can see, in this configuration the datasource configuration is explicitly written into the xml configuration file.
How can I obtain it from JBoss through the defined JNDI name?
You can use following spring-servlet.xml:
<jee:jndi-lookup id="dataSource" jndi-name="<JNDI NAME>" />
add following in xsi:schemaLocation attribute of beans tag :
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

Configure Hibernate C3P0 Connection Pooling

I'm stumbled upon a problem while developing a Web Application based on Struts2 with Spring and Hibernate.
When I refresh the site a few times, for example 2-4 times, Hibernate is showing up an exception about Too many connections. I've tried to implement C3P0 Connection pool and have some problems with it
The hibernate.cfg.xml configuration:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/axelle</property>
<property name="hibernate.connection.username">axelle</property>
<property name="hibernate.connection.password">dbpassword</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
applicationContext.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:connectionProperties="${jdbc.connectionProperties}"/>
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
The log output is:
org.hibernate.exception.JDBCConnectionException: Cannot open connection
and:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
And this is how PROCESSLIST MySQL window looks: http://img844.imageshack.us/img844/3959/be69273cc2.png
I've set max_size of connections to 20, but it seems like it doesn't read the C3P0 configuration from file, cause from the screen we can see that number of connections is higher than 20, or maybe I'm doing something wrong, but where? I really need some help guys, I'll appreciate this, and thanks in advance.
Mention these property in your hibernate.cfg.xml file
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.min_size">10</property>
<property name="hibernate.c3p0.timeout">100</property>
Refer this link for better understanding: Configuration Properties
In your applicationContext, instead of using <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" ... try using <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ...
The C3P0PooledDataSource will create a wrapped database connection using the specified DriverClassName, url, username and password

spring + hibernate + infinispan as 2nd level cache

I tried to setup infinispan as 2nd level cache for hibernate in spring+tomcat based app.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driver_class}"/>
<property name="url" value="${hibernate.connection.url}"/>
<property name="username" value="${hibernate.connection.username}"/>
<property name="password" value="${hibernate.connection.password}"/>
<property name="maxActive" value="${hibernate.connection.maxActive}"/>
<property name="maxIdle" value="${hibernate.connection.maxIdle}"/>
<property name="minIdle" value="${hibernate.connection.minIdle}"/>
<property name="maxWait" value="${hibernate.connection.maxWait}"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager">
<bean class="com.arjuna.ats.jta.TransactionManager" factory-method="transactionManager"/>
</property>
<property name="userTransaction">
<bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.example"/>
<!---->
<property name="hibernateProperties" ref="db-properties"/>
</bean>
And the properties are:
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.infinispan.InfinispanRegionFactory
I get an exception:
Caused by: org.infinispan.CacheException: This is transactional cache but no transaction manager could be found. Configure the transaction manager lookup properly.
How do I configure a transaction manager lookup?
The easy answer for spring is to add (inside id="sessionFactory" spring XML configuration) :
<property name="jtaTransactionManager" ref="transactionManager"/>
This is available in spring 3.2.2.RELEASE (it maybe available in older spring versions too, I would guess since spring 3.1.x)
This should achieve what Steve Ebersole points at. This causes Spring to provide a JtaPlatform via the class https://github.com/SpringSource/spring-framework/blob/master/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
See JavaDoc for class org.springframework.orm.hibernate4.LocalSessionFactoryBean (that you are using) concerning JTA usage.
Both Hibernate and Infinispan need to know about the TransactionManager. If you tell Hibernate about the TransactionManager, it will in turn tell Infinispan for you. I am guessing Spring has a way to tell Hibernate about the JTA setup, but I could not find it. Hibernate for its part (I see you are at least trying to use Hibernate 4) needs to be told about which org.hibernate.service.jta.platform.spi.JtaPlatform to use. org.hibernate.service.jta.platform.spi.JtaPlatform is the contract for Hibernate to know how to interact with the JTA environment.

Error upon integrating JNDI with Spring

This was my first attempt at Spring with JNDI but getting the below mentioned exception when trying to create the ApplicationContext like:
ApplicationContext context = new ClassPathXmlApplicationContext("master-job.xml");
The Spring configuration file is as follows:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="/jdbc/Eqpstatus"/>
<property name="resourceRef" value="true" />
</bean>
<bean id="masterDao" class="com.dao.MasterDao">
<property name="dataSource" ref="dataSource"/>
</bean>
On Server i have the required resource entry for the JNDI name.
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver"
maxActive="10" maxIdle="2" maxWait="10000" name="jdbc/Eqpstatus"
password="xxxx" type="javax.sql.DataSource"
url="jdbc:oracle:thin:#(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xyz)))"
username="xxx"/>
The error i see is:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
Would highly appreciate any inputs on this as am new to Spring-JNDI integration.
First, I think you should use the dedicated tag instead of declaring a "JndiObjectFactoryBean" bean :
<!-- JNDI DataSource for J2EE environments -->
<jee:jndi-lookup id="dataSource" jndi-name="java:jdbc/rppsDS-PUB-PROTO" default-ref="localDataSource" />
<!-- local dataSource for JUnit integration tests -->
<bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
Then, you need a jndi.properties file (which could be directly in application server dir such as JBoss) with a content similar to :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
If you are using Tomcat and everything is fine with your Tomcat configuration; this should be enough:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/dataSource" />
Where jdbc/dataSource is the name defined in your Tomcat config.
EDIT:
My bad, forgot about the jndi.properties; there are two possibilities:
either provide a jndi.properties on your classpath or
set the values in a in the <jee:jndi-environment/> element of <jee:jndi-lookup /> (see reference)
The java.naming.factory.initial property needs to be set for sure, to something like org.apache.naming.java.javaURLContextFactory, possibly some other values as well, like:
java.naming.factory.url.pkgs=org.apache.naming
java.naming.factory.url.pkgs.prefixes=org.apache.naming
java.naming.provider.url=org.apache.naming
Also see reference.

Resources