I use following line of code to read config.properties file in my spring mvc configuration servlet xml file.
<context:property-placeholder location="file:///${config.properties}" />
config.properties contains a property say: propertyName = propertyValue
I want to use this propertyName's value in spring mvc configuration servlet xml file.
Here is an example of configuring as pool with properties taken from a properties file
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="validationQuery" value="SELECT 1 FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="300000" />
<property name="numTestsPerEvictionRun" value="6" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="initialSize" value="3" />
<property name="maxActive" value="75" />
<property name="maxIdle" value="75" />
<property name="maxWait" value="5000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="100" />
</bean>
The property file contains there rows
database.url=localhost:1521:xe
database.username=dbusername
database.password=dbpassword
I guess you need to add
<context:property-placeholder location="file:///${config.properties}" />
to your servlet.xml file.
Related
<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://xxx.xx.xxx.xxx:3306/vod6?autoReconnect=true" />
<!-- <property name="url" value="jdbc:mysql://xxx.xx.xxx.xxx:3306/vod2?autoReconnect=true" /> -->
<property name="username" value="voddb" />
<property name="password" value="vod#123" />
</bean>
above config xml working fine .
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${mysql.driverClassName}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
When I am trying to configure jdbc.property and configure them throwing error out of memory
Can you please explain what i am missing?
mysql.database=MYSQL
mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://xxx.xx.xxx.xxx:3306/vod?autoReconnect=true
mysql.username=voddb
mysql.password=vod#123
mysql.initialSize=4
mysql.maxActive=30
property file
Please confirm whether the placeholder is mentioned correctly in applicationContext.xml file
<context:property-placeholder location="classpath*:catalina.properties" ignore-unresolvable="true"/>
If this is configured correctly please try removing mysql.initialSize=4 mysql.maxActive=30 from properties file and test, this may be thr root cause. Because this is the additional property you have mentioned in properties file
You can try configuring these maxActive,initialSize values in xml itself as property.
<property name="removeAbandonedTimeout" value="180"/>
<property name="removeAbandoned" value="true"/>
<property name="logAbandoned" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="5000"/>
<property name="minEvictableIdleTimeMillis" value="900000"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="4"/>
I am using spring MVC 4.2 I have this code in my app-context.xml
<bean id="beanConstants" name="beanConstants" class="com.my.web.controller.BeanConstants">
<property name="dbProplocation" value="/my/database.properties" />
<property name="extractDbProplocation" value="/my/extract.database.properties" />
<property name="cssLocation" value="uncompiled" />
<property name="enableSuspensionPollingStr" value="false" />
</bean>
<!-- JDBC Session Config -->
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
Now this all worked fine until I added the JdbcHttpSessionConfiguration bean. If I take that line out, it works. otherwise I get this:
Caused by: java.lang.IllegalArgumentException:
Could not resolve placeholder 'hibernate.connection.url' in string value "${hibernate.connection.url}"
I am trying to save the sesstion info in the DB. Why is this doing this? What has one got to do with the other?
I'm failling to use Spring4 togather with Hibernate4 in a standalone process (no container like tomcat, WAS, ...)
How can I use Hibernate4, Spring4 and Spring data repositories togather in a standalone process?
However I confiugre Spring I allways get the same exception:
Caused by: java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
When googling for this, I get pointet to some information about hibernate.transaction.jta.platform and the docu for Hibernate 4.3 is here http://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html_single/#services-JtaPlatform
But the only option I see for my case would be org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform but this still leads to the same error.
Here is my Spring config:
<context:component-scan base-package="com.xx.yy" />
<jpa:repositories base-package="com.xx.zz.respositories"></jpa:repositories>
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/culture" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jtaDataSource" ref="dataSource" />
<property name="packagesToScan" value="culture.matching.index.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<property name="database" value="MYSQL" />
</bean>
</property>
<property name="jpaProperties">
<value>
hibernate.transaction.jta.platform=org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform
</value>
</property>
</bean>
Answer by #geoand helped a lot: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-simple
I therefore moved from XML to Java config
Following is the my configuration file for C3p0 connection pool. But after some time on starting my application all the connections are reaching to max connections i.e 200 and sitting in idle after that there are no connections are being used by application and if I hit my application URL from browser it is says that service temp unavailable.
My application is client-socket connection application, on opening the connection from client I am doing the data insertions and updations on DB.
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl"
value="jdbc:postgresql://localhost/dbname" />
<property name="user" value="developer" />
<property name="password" value="developerPassword" />
<!-- <property name="initialPoolSize" value="20"/> -->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="200" />
<property name="maxIdleTime" value="1" />
<property name="maxAdministrativeTaskTime" value="900" />
<property name="maxIdleTimeExcessConnections" value="2" />
<!-- <property name="checkoutTimeout" value="300"/> -->
<!-- <property name="maxStatements" value="500"/> -->
<property name="testConnectionOnCheckin" value="true" />
<property name="maxConnectionAge" value="60" />
<property name="unreturnedConnectionTimeout" value="1000" />
<property name="debugUnreturnedConnectionStackTraces" value="true" />
</bean>
Spring3, Hibernate, MySQL: I am working on my first project, can some one please show me how to change the following code to add database pooling? thanks
applicationContext-security-JDBC.xml
<beans:bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${database.driver}" />
<beans:property name="url" value="${database.url}" />
<beans:property name="username" value="${database.user}" />
<beans:property name="password" value="${database.password}" />
</beans:bean>
jdbc.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1/db_mytest
database.user=root
database.password=
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
thanks.... again
I suggest you to use commons-dbcp. You have to download the jar and add it to the WEB-INF/lib directory (if not already included in your application server). This is your new applicationContext.xml with some default parameters that you should change as you wish:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<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="timeBetweenEvictionRunsMillis" value="300000" />
<property name="numTestsPerEvictionRun" value="6" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="initialSize" value="3" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="5000" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="100" />
</bean>
Hope this helps.
You don't specify what you are running your application on. Many application servers have their own connection pooling implementation that you can tap into. I've used Tomcat's DBCP in the past - was quite easy to set up.