java.sql.SQLException: ORA-06576: not a valid function or procedure name - spring

When saveDepartment() is invoked, I am getting exception mentioned in the title. After searching for solution for a while I came up with another similar post on stackoverflow which doesn't match the problem scenario I am facing.
Dao class:
#Repository
public class DepartmentDaoImpl implements DepartmentDao {
#Autowired
private SessionFactory sessionFactory;
#Override
#Transactional
public void saveDepartment(Department department) {
Session session = sessionFactory.getCurrentSession();
session.save(department);
}
}
Bean configuration section for hibernate:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="net.therap.domain.tmp"/>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Any suggestion or solution regarding the problem is appreciated.

ORA-06576 error code and oracle11g tag are suggesting you're using Oracle 11g Database.
Hibernate's Oracle10gDialect is compatible with that version, so you should use following dialect configuration:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="net.therap.domain.tmp"/>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

Related

Integration of Spring 5+ Hibernate 5 + Atomikos 4 + Tomcat Unable to locate current JTA transaction

I have tried many different solutions, but getting the exception:
org.hibernate.HibernateException: Unable to locate current JTA
transaction at
org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:75)
~[hibernate-core-5.2.12.Final.jar:5.2.12.Final] at
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:465)
~[hibernate-core-5.2.12.Final.jar:5.2.12.Final]
I am using Atomikos 4.0.4, Hibernate 5.2.12, Spring 5.0.2 and Tomcat.
How to configure Spring 5 + Hibernate 5
This is my configuration
application-context.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache/ehcache.xml</prop>
<prop key="hibernate.connection.datasource">java:/comp/env/jdbc/myoracle</prop>
<prop key="hibernate.transaction.jta.platform">cgaweb.common.atomikos.SpringJtaPlatformAdapter</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="javax.persistence.transactionType">jta</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.id.new_generator_mappings">true</prop>
<prop key="hibernate.mapping.precedence">class, hbm</prop>
<prop key="connection.driver_class">oracle.jdbc.OracleDriver</prop>
</props>
</property>
</bean>
<!-- jta transaction -->
<bean id="atomikosTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp" init-method="init" destroy-method="shutdownWait" >
<constructor-arg>
<props>
<prop key="com.atomikos.icatch.log_base_name">UserTransactionServiceImplog</prop>
</props>
</constructor-arg>
</bean>
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close" depends-on="atomikosTransactionService">
<property name="forceShutdown" value="true"/>
<property name="transactionTimeout" value="300" />
</bean>
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" depends-on="atomikosTransactionService">
<property name="TransactionTimeout" value="300"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"></property>
<property name="userTransaction" ref="atomikosUserTransaction"></property>
<property name="allowCustomIsolationLevels" value="true"></property>
</bean>
<!-- Manager lookup-->
<bean id="springJtaPlatformAdapter" class="cgaweb.common.atomikos.SpringJtaPlatformAdapter">
<property name="jtaTransactionManager" ref="transactionManager" />
</bean>
<!-- post construct -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
the code of springJtaPlatformAdapter
public class SpringJtaPlatformAdapter extends AbstractJtaPlatform {
private static TransactionManager sTransactionManager;
private static UserTransaction sUserTransaction;
#Override
protected TransactionManager locateTransactionManager() {
return sTransactionManager;
}
#Override
protected UserTransaction locateUserTransaction() {
return sUserTransaction;
}
public void setJtaTransactionManager(JtaTransactionManager jtaTransactionManager) {
sTransactionManager = jtaTransactionManager.getTransactionManager();
sUserTransaction = jtaTransactionManager.getUserTransaction();
}
public void setTransactionManager(TransactionManager transactionManager) {
sTransactionManager = transactionManager;
}
public void setUserTransaction(UserTransaction userTransaction) {
sUserTransaction = userTransaction;
}
}
i am using tomcat, and i add the following lib :
transaction-jta-4.0.4.jar
transaction-jdbc.4.0.4.jar
transaction-hibernate4-4.0.jar
transaction-api-4.0.4.jar
transactions-4.0.4.jar
jta-1.1.jar
can you please help me to solve this issue ?
Thanks
The commercial edition of Atomikos contains built-in Tomcat integration and Hibernate 5 support:
https://www.atomikos.com/Main/ExtremeTransactionsFreeTrial
Alternatively, you can wait for the 5.0 release of TransactionsEssentials where Hibernate 5 demos would also be included. I will not have built-in Tomcat integration though.
Best regards

Unable to save Student model object using HibernateTemplate save method in DAOImplementation class

this is exception i am getting while executing spring application.can anyone help me to resolve this problem.
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: drop table if exists postgres.student007 cascade
Hibernate: create table postgres.student007 (id int4 not null,address varchar(255), email varchar(255), name varchar(255), primary key (id) Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
at dao.StudentDaoImplHT.save(StudentDaoImplHT.java:22)
at test.Client.main(Client.java:21)
below is my configuration config.xml file.can anyone help me to resolve this problem.
<beans>
<bean id="bds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
<property name="maxActive" value="15"/>
<property name="maxIdle" value="5"/>
<property name="maxWait" value="5000"/>
</bean>
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="bds"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.Student</value>
</list>
</property>
</bean>
<bean id="ht" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dao" class="dao.StudentDaoImplHT">
<property name="ht" ref="ht"/>
</bean>
</beans>
It's the problem of spring transaction. You need to config transaction
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Then enable the annotation configure for transaction
<tx:annotation-driven />
At last, add the transaction configure in your method.
#Transactional(propagation = Propagation.REQUIRED, readOnly = false)
You problem is that you didn't use the transaction but do writing things into database. It's not allowed, you should create a transaction and make the readOnly = false. Then you can create table.
You can use jdbcTemplate.execute(sql). to create table.

weblogic jndi datasource issue with spring application

I have created a oracle datasource in weblogic with the name jdbc/myDS.
Weblogic created a xml file in mydomain/config/jdbc and the configuration works from the weblogic admin console. Test connection is working. My spring context file details are:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDS"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<util:map>
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.show_sql" value="true" />
</util:map>
</property>
</bean>
<bean id="myDAO" class ="com.example.MyDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
My Java class is:
public class MyDAOImpl implements MyDAO{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void persistPerson(Person person) {
Session session = getSessionFactory().openSession();
try {
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
}catch(HibernateException he) {
he.printStackTrace();
} finally {
session.close();
}
}
}
An error occurred during activation of changes, please see the log for
details.
Message icon - Error weblogic.application.ModuleException:
Message icon - Error While trying to lookup 'jdbc.myDS' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/myDS'
The following provides a Spring XML configuration to obtain a datasource by a JNDI name and inject it into Springs AnnotationSessionFactoryBean.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/YourJndi"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>com.java.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
This example has been sucessfully tested on Weblogic 10.3.4 with Oracle 11g.

Spring + Hibernate4 Error CurrentSessionContext is always null

I'm using Hibernate 4 with spring 3.1 in a simple java Apllication.
I use the following code to create the Spring SessionFactory and then convert it into a hibernate SessionFactory:
pls waht is missing here ... Is this the right way to go?? Or do I miss something..? pls help!
.....
......
context=new ClassPathXmlApplicationContext(new String[]{"spring.xml"});
return (SessionFactory) context.getBean("mySessionFactory");
......
The CurrentSessionContext ofthe sessionfactory is always null!
So I cant execute
sessionFactory.getcurrentSession()
-> gives me an java.lang.NullPointer Exception
myBean Declarations in spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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=""/>
</bean>
<bean id="mySessionFactory" name="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>TblUrls.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.SunOneJtaPlatform</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref local="mySessionFactory" />
</property>
</bean>
<bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name = "sessionFactory" ref = "mySessionFactory" />
</bean>
<!-- <bean id="myProductDao" class="hib.TblUrlsHome"> -->
<!-- <property name="sessionFactory" ref="mySessionFactory"/> -->
<!-- </bean> -->
</beans>
You have to do
sessionFactory.openSession();
This should solve your problem.

Spring, eclipselink, compass integration

I went through several articles and configured the following, but i can see some problems with transaction management. Please let me know whether i'm using compass correctly in the below configurations:
in Spring-config:
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean class="org.compass.spring.support.CompassContextBeanPostProcessor"/>
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="classMappings">
<list>
<value>......</value>
<value>......</value>
<value>......</value>
</list>
</property>
<property name="compassSettings">
<props>
<prop key="compass.engine.connection">file:///usr/local/lucene</prop>
<prop key="compass.transaction.factory">
org.compass.spring.transaction.SpringSyncTransactionFactory
</prop>
</props>
</property>
<property name="transactionManager" ref="transactionManager"/>
</bean>
in compass code:
private CompassSession session;
#CompassContext
protected void setCompassSession(CompassSession session) {
this.session = session;
}
public void index(Coupon coupon) throws AppException{
try {
session.save(coupon);
} catch (CompassException exception) {
logger.debug("Error in coupon indexing: "+ exception.getMessage());
}
}
in Service Layer:
#Transactional
public void saveCoupon(Coupon coupon) throws AppException{
Coupon savedCoupon = dbCouponDAO.saveCoupon(coupon); // saves to db through eclipselink
nonDbCouponDAO.index(savedCoupon); // indexes in compass
}
As per my understanding, transaction manager configured to eclipselink in spring can be used as compass transaction manager too, and both eclipselink and compass operations can be used in a single unit as i have mentioned in service layer.
Please let me know if i'm doing anything wrong here.
Thanks.
Try to add this,
<!-- Search Manager using Comass abstractions. -->
<bean class="org.compass.spring.support.CompassContextBeanPostProcessor"/>
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="compassSettings">
<props>
<prop key="compass.engine.connection">file://${user.home}/indexes</prop>
<prop key="compass.transaction.factory">org.compass.spring.transaction.SpringSyncTransactionFactory</prop>
</props>
</property>
<property name="classMappings">
<list>
<value>MyEntity</value>
</list>
</property>
<property name="transactionManager">
<ref local="transactionManager" />
</property>
</bean>
<bean id="jpaGpsDevice" class="org.compass.gps.device.jpa.JpaGpsDevice">
<property name="name">
<value>jpaDevice</value>
</property>
<property name="entityManagerFactory">
<ref local="entityManagerFactory" />
</property>
<property name="nativeExtractor">
<bean class="org.compass.gps.device.jpa.extractor.SpringNativeJpaExtractor" />
</property>
</bean>
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps" init-method="start" destroy-method="stop">
<property name="compass">
<ref bean="compass" />
</property>
<property name="gpsDevices">
<list>
<ref bean="jpaGpsDevice" />
</list>
</property>
</bean>
<!-- COMPASS END -->
Let me know if this helps.

Resources