<? XML version = "1.0" encoding = "GBK"?> <! -- Specify the DTD information of the spring configuration file --> <! Doctype beans public "-// spring // DTD bean 2.0 // en" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <! -- Root element of the spring configuration file --> <beans> <! -- Define the data source bean and use the c3p0 Data Source implementation --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource" Destroy-method = "close"> <! -- Specify the driver to connect to the database --> <property name = "driverclass" value = "com. MySQL. JDBC. Driver"/> <! -- Specify the URL to connect to the database --> <property name = "jdbcurl" value = "JDBC: mysql: // localhost/javaee"/> <! -- Specify the username used to connect to the database --> <property name = "user" value = "root"/> <! -- Specify the password to connect to the database --> <property name = "password" value = "32147"/> <! -- Specify the maximum number of connections to the database connection pool --> <property name = "maxpoolsize" value = "40"/> <! -- Specify the minimum number of connections to the database connection pool --> <property name = "minpoolsize" value = "1"/> <! -- Specify the number of initial connections to the database connection pool --> <property name = "initialpoolsize" value = "1"/> <! -- Specify the maximum idle time for connection to the database connection pool --> <property name = "maxidletime" value = "20"/> </bean> <! -- Configure the local Transaction Manager of the JDBC data source and use the datasourcetransactionmanager class --> <! -- This class implements the platformtransactionmanager interface, which is specific to the use of data source connection --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. cetcetransactionmanager"> <! -- When configuring cetcetransactionmanager, you must reference datasource injection. --> <property name = "datasource" ref = "datasource"/> </bean> <! -- Configure a business logic Bean --> <bean id = "test" class = "Lee. testimpl "> <property name =" ds "ref =" datasource "/> </bean> <! -- Configure transaction proxy for the business logic Bean --> <bean id = "testtrans" class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean"> <! -- Inject Transaction Manager into the transaction proxy factory Bean --> <property name = "transactionmanager" ref = "transactionmanager"/> <property name = "target" ref = "test"/> <! -- Specify transaction properties --> <property name = "transactionattributes"> <props> <prop key = "*"> propagation_required </prop> </props> </property> </ bean> </beans>
public interface Test{ public void insert(String u);}
Public class testimpl implements test {private datasource Ds; Public void setds (datasource DS) {This. DS = Ds;} public void insert (string U) {jdbctemplate Jt = new jdbctemplate(ds1_1_jt.exe cute ("insert into mytable values ('" + u + "')"); // Insert the same data twice, and disable the illegal primary key into jt.exe cute ("insert into mytable values ('" + u + "')"); // If transaction control is added, the first record cannot be inserted. // If there is no transaction control, the first record can be inserted }}
Public class maintest {public static void main (string [] ARGs) {// create spring container applicationcontext CTX = new classpathxmlapplicationcontext ("bean. XML "); // obtain the transaction proxy bean test T = (TEST) CTX. getbean ("testtrans"); // execute the insert operation T. insert ("BBB ");}}
Transactionproxyfactorybean proxy transaction