I am using the below statement in my applicationContext.xml file (Spring 3).
<bean name="loggingAutoProxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
In this applicationContext file, when i try to give a different name for the bean property name and reference, it is returning null object. But, if i give a same value for the name and reference, it is working properly/ returning the bean reference correctly. Is this problem because of the org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator? If yes, how to resolve this issue?
This doesn't work (returns the bean reference as null in my java code):
<bean id="cacheDelegate" class="com.srsa.wiradmin.delegate.CacheDelegate" >
<property name="statusDBDAO"><ref bean="BMOStatusDBDAO" /></property>
</bean>
<bean id="BMOStatusDBDAO" class="com.srsa.wiradmin.dao.StatusDBDAO">
<property name="dataSource">
<ref bean="BMOAdminDataSource" />
</property>
</bean>
<bean id="BMOAdminDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" lazy-init="default" autowire="default" dependency-check="default">
<property name="driverClass" value="${jdbc.driver}" ></property>
<property name="jdbcUrl" value="${admin.jdbc.url}" ></property>
<property name="user" value="${admin.jdbc.user}" ></property>
<property name="password" value="${admin.jdbc.password}" ></property>
<property name="initialPoolSize" value="3" ></property>
<property name="minPoolSize" value="3" ></property>
<property name="maxPoolSize" value="25" ></property>
<property name="acquireIncrement" value="1" ></property>
<property name="acquireRetryDelay" value="1000" ></property>
<property name="debugUnreturnedConnectionStackTraces" value="true" ></property>
<property name="maxIdleTime" value="300" ></property>
<property name="unreturnedConnectionTimeout" value="300000" ></property>
<property name="preferredTestQuery" value="SELECT COUNT(*) FROM LOCALE_CODE" ></property>
<property name="checkoutTimeout" value="300000" ></property>
<property name="idleConnectionTestPeriod" value="600000" ></property>
</bean>
This works (returns the object properly):
<bean id="cacheDelegate" class="com.srsa.wiradmin.delegate.CacheDelegate" >
<property name="statusDBDAO"><ref bean="statusDBDAO" /></property>
</bean>
<bean id="statusDBDAO" class="com.srsa.wiradmin.dao.StatusDBDAO">
<property name="dataSource">
<ref bean="BMOAdminDataSource" />
</property>
</bean>
<bean id="BMOAdminDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" lazy-init="default" autowire="default" dependency-check="default">
<property name="driverClass" value="${jdbc.driver}" ></property>
<property name="jdbcUrl" value="${admin.jdbc.url}" ></property>
<property name="user" value="${admin.jdbc.user}" ></property>
<property name="password" value="${admin.jdbc.password}" ></property>
<property name="initialPoolSize" value="3" ></property>
<property name="minPoolSize" value="3" ></property>
<property name="maxPoolSize" value="25" ></property>
<property name="acquireIncrement" value="1" ></property>
<property name="acquireRetryDelay" value="1000" ></property>
<property name="debugUnreturnedConnectionStackTraces" value="true" ></property>
<property name="maxIdleTime" value="300" ></property>
<property name="unreturnedConnectionTimeout" value="300000" ></property>
<property name="preferredTestQuery" value="SELECT COUNT(*) FROM LOCALE_CODE" ></property>
<property name="checkoutTimeout" value="300000" ></property>
<property name="idleConnectionTestPeriod" value="600000" ></property>
</bean>
bye....
Your cacheDelegate bean is looking up a bean referenced to the ID statusDBDAO, but the ID on the DAO is actually BMOStatusDBDAO.
Related
persistence.xml
<persistence-unit name="user_per_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.example.User</class>
<class>com.example.Order</class>
<class>com.example.Package</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.jdbc.batch_size" value="100" />
<property name="hibernate.order_inserts" value="true" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
applicationContext.xml
<jpa:repositories base-package="com.example.user.repository" entity-manager-factory-ref="userEntityManagerFactory" transaction-manager-ref="userTransactionManager"/>
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="user_per_unit" />
<property name="dataSource" ref="userDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<bean id="userTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="userEntityManagerFactory" />
</bean>
<bean id="userDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${size}" />
<property name="maxTotal" value="${maxtotal}" />
<property name="maxIdle" value="${maxidle}" />
<property name="minIdle" value="${minidle}" />
</bean>
Class
#Autowired
private ApplicationContext applicationContext;
private JpaTransactionManager userTransactionManager;
private EntityTransaction userTx;
userTransactionManager = (JpaTransactionManager) applicationContext.getBean("provTransactionManager");
userTx = provTransactionManager.getEntityManagerFactory().createEntityManager().getTransaction();
userTx.begin();
boolean isUpdateSuccess = updateViaUserRepository(...);
if(!isUpdateSuccess) {
userTx.rollback();
} else {
userTx.commit();
}
My code is reaching userTx.rollback(); but actual database is not getting rolledback. Is there any mistake in my persisence or applicationContext configurations.
Edit: All my tables have engine as InnoDb
First thing to look for: what is the table type that you are using?
For example when it comes to MySQL, MyISAM tables do not support transactions, which means you have to use another table type (e.g. InnoDB).
I need to use 2 version of DB2Driver for my application. I am using spring to define dataSources. The path of two Libs are the same, the first dataSource needs driver version2 and second one needs version3. Is there any way to solve this problem?
<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="yyy"/>
<property name="username" value="yyy"/>
<property name="password" value="yyy"/>
</bean>
<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="xxx"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="1"/>
<property name="maxWait" value="3000"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="10000"/>
<property name="numTestsPerEvictionRun" value="50"/>
<property name="minEvictableIdleTimeMillis" value="10000"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
I have deployed two Spring roo application on Tomcat 7 server both have database connection while hitting any service of application I am getting below exception
Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL 'postgres://username:password#localhost:5432/db'
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:278)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
Please find below database base configuration
Application 1
application-Context.xml
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 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}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Application 1. Database.properties
database.driverClassName=org.postgresql.Driver
database.url=jdbc\:postgresql\://localhost\:5432/db
database.username=username
database.password=password
Application 2
application-Context.xml
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="postgres://username:password#localhost:5432/db"/>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="#{ 'jdbc:postgresql://' + #dbUrl.getHost() + ':' + #dbUrl.getPort() + #dbUrl.getPath() }"/>
<property name="username" value="#{ #dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ #dbUrl.getUserInfo().split(':')[1] }"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT version();"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<util:properties id="regex.properties" location="classpath:META-INF/spring/regex.properties"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Please advice how can I resolve my Problem..
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/db" />
<property name="user" value="user" />
<property name="password" value="pass" />
<!-- pool sizing -->
<property name="initialPoolSize" value="3" />
<property name="minPoolSize" value="6" />
<property name="maxPoolSize" value="25" />
<property name="acquireIncrement" value="3" />
<property name="maxStatements" value="0" />
</bean>
I used this and it works
How to declare useFetchSizeWithLongColumn=true with Hibernate? either under com.mchange.v2.c3p0.ComboPooledDataSource or org.apache.commons.dbcp.BasicDataSource in a properties bean configuration file?
I already tried:
<bean id="oracle_prop" lazy-init="false"
class="org.springframework.util.StringUtils"
factory-method="collectionToDelimitedString">
<constructor-arg index="0">
<list>
<value>oracle.jdbc.useFetchSizeWithLongColumn=true</value>
</list>
</constructor-arg>
<constructor-arg value=";" index="1" type="java.lang.String"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="initialSize" value="100"/>
<property name="maxIdle" value="100"/>
<property name="maxActive" value="1000"/>
<property name="connectionProperties" ref="oracle_prop"/>
</bean>
and
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="properties" ref="dataSourceProperties"></property>
<property name="driverClass" value="${database.driver}"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="initialPoolSize" value="100"/>
<property name="minPoolSize" value="100"/>
<property name="maxPoolSize" value="1000"/>
<property name="maxIdleTime" value="1800"/>
<property name="maxStatements" value="50"/>
</bean>
<bean id="dataSourceProperties" class="java.util.Properties">
<constructor-arg>
<props>
<prop key="oracle.jdbc.useFetchSizeWithLongColumn">true</prop>
</props>
</constructor-arg>
</bean>
and
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${database.driver}"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="properties">
<props>
<prop key="user">${database.username}</prop>
<prop key="password">${database.password}</prop>
<prop key="useFetchSizeWithLongColumn">true</prop>
</props>
</property>
<property name="initialPoolSize" value="100"/>
<property name="minPoolSize" value="100"/>
<property name="maxPoolSize" value="1000"/>
<property name="maxIdleTime" value="1800"/>
<property name="maxStatements" value="50"/>
</bean>
<property name="connectionProperties" ref="oracle_prop"/>
This will not work as it will try to bind "object" or "property" type object to connectionProperties where as setConnectionPropeties(String args) expect a string argument. Try changing ref with value and it should inject property as String data type. Else we can also pass properties delimited with ";".
Workaround
Look for all the long data types and change each manually.
Select * from user_tab_columns c where c.DATA_TYPE = 'LONG';
Sad to say useFetchSizeWithLongColumn will not work everytime.
So if you want to use this property, make sure that the LONG columns you are retrieving are not too big or you may run out of memory.
For JPA Hibernate, you could also add #Lob Annotation.
I am learning spring 3.0 from Spring in Action.
There it talks about importance of having jpadialect in JpaTranactionManager
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaDialect"
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
However the dialect is already present in declaration of entityManagerFactory via jpaVendorAdaptor.
<bean id="entityManagerFactory" class= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="HSQL" />
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform"
value="org.hibernate.dialect.HSQLDialect" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource. DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:hsql://localhost/spitter/spitter" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
Is the use of dialect in JpaTranactionManager redundant?
The use of jpaDialect in JpaTransactionManager is no redundant to jpaVendorAdapter config. Their config have different purpose.
You can refer this post with good explanation.