//When configuring the database, pay attention
to the automatic configuration of the dbcp data source transaction, otherwise it cannot be submitted
<property name="defaultAutoCommit">
<value>false</value>
</property>
Usage 1:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/ beans/spring-beans-2.5.xsd">
<!-- Use the PropertyPlaceholderConfigurer provided by spring to read database configuration information.properties -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!—
1. The classpath here can be considered as the src in the project-
2. The property name is locations, and the sub-tag <list></list> can be used to specify the configuration files of multiple databases. One is specified here
->
<value>classpath:resource/config/jdbc.properties</value>
</ list>
</property>
</bean>
The database configuration file project path at this time is like this.
Usage 2: To
read the database configuration file, you can also use the following method
<?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-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config .PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config_test/jdbc.properties</value>
</list>
</property>
</bean>
jdbc.properties file at this time The location is shown in the figure below.
There can be multiple properties configuration files. Here, two data configuration files are specified in the <list></list> tags
< ?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-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>/WEB-INF/config_test/jdbc.properties</value>
</list>
</property>
</bean>
classpath:jdbc.The file location corresponding to properties is: sqlserver.password=sqlserver sqlserver.username=sa
The content of the file is: The connection information of sqlserver is configured
sqlserver.url=jdbc\:jtds\:sqlserver\://localhost\:1433/J2EE
sqlserver.driver=net.sourceforge.jtds.jdbc.Driver
/WEB-INF/config_test/jdbc.properties The corresponding file location is
file The content is: The connection information of oracle is configured
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=jxbms
jdbc.password=
jxbms After the configuration information of the database is read, it can be used when creating a datasource. Connect to oracle
below and use apache's dbcp data source
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method ="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
< /property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxIdle">
<value>3</value>
</property>
<property name="maxWait">
<value>-1</value>
</property>
<property name="defaultAutoCommit">
Then I thought that I could finally breathe a sigh of relief now, who knew that the sky was not what people wanted. When a table is replaced for testing, the data in the database still does not change. Is the c3p0 data source not easy
to use? After being tortured by the code again, I
finally found that the test java file was changed, not in a project, and other projects were closed. That's good.
When the document was written here, I suddenly found that the dbcp data source used by oracle has this configuration
<property name="defaultAutoCommit">
<value>false</value>
</property>
The automatic transaction of the original dbcp data source The commit function is turned off.
Immediately change the transaction auto-commit to true for testing, everything is ok, (^ _ ^)
Loading of .properties files, and application of el expressions
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326986857&siteId=291194637
Recommended
Ranking