标签:接口 last 最简 wal static cas 新建 equal user
create table account( id int primary key auto_increment, name varchar(20), money double ); insert into account values(null,‘aaa‘,1000); insert into account values(null,‘bbb‘,1000); insert into account values(null,‘ccc‘,1000);
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql:///mydb1 username=root password=root #driverClass=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:XE #username=system #password=system
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; //使用配置文件 public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { // 2.获取连接 Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); return con; } //关闭操作 public static void closeConnection(Connection con) throws SQLException{ if(con!=null){ con.close(); } } public static void closeStatement(Statement st) throws SQLException{ if(st!=null){ st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException{ if(rs!=null){ rs.close(); } } }新建简单测试类TransactionTest1.java
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //jdbc中事务操作 public class TransactionTest1 { public static void main(String[] args) throws SQLException { // 修改id=2这个人的money=500; String sql = "update account set money=500 where id=2"; Connection con = JdbcUtils.getConnection(); con.setAutoCommit(false); //开启事务,相当于 start transaction; Statement st = con.createStatement(); st.executeUpdate(sql); //事务回滚 //con.rollback(); con.commit(); //事务提交 st.close(); con.close(); } }
package cn.itcast.transaction; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import cn.itcast.utils.JdbcUtils; //jdbc中事务操作 public class TransactionTest2 { public static void main(String[] args) { // 修改id=2这个人的money=500; String sql = "update account set money=500 where id=1"; Connection con = null; Statement st = null; try { con = JdbcUtils.getConnection(); con.setAutoCommit(false); // 开启事务,相当于 start transaction; st = con.createStatement(); st.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); // 事务回滚 try { con.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { con.commit(); // 事务提交 st.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package cn.itcast.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.itcast.exception.AccountException; //没有使用ThreadLocal来获取Connection。 public class _AccountDaoImpl { // 从accountOut账户转出money public void accountOut(Connection con, String accountOut, double money) throws SQLException, AccountException { String sql = "update account set money=money-? where name=?"; PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountOut); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转出失败"); } pst.close(); } // 向accountIn账户转入money public void accountIn(Connection con, String accountIn, double money) throws SQLException, AccountException { String sql = "update account set money=money+? where name=?"; PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountIn); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转入失败"); } pst.close(); } }真实的实现类实现自别人设计的DAO接口,一般不会考虑到Connection
package cn.itcast.dao; import java.sql.Connection; public interface AccountDao { public void accountOut(String accountOut, double money) throws Exception; public void accountIn(String accountIn, double money) throws Exception; }真实DAO实现类实现了AccountDao接口
package cn.itcast.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import cn.itcast.exception.AccountException; import cn.itcast.utils.JdbcUtils; public class AccountDaoImpl implements AccountDao { // 从accountOut账户转出money public void accountOut(String accountOut, double money) throws SQLException, AccountException { String sql = "update account set money=money-? where name=?"; Connection con=JdbcUtils.getConnection(); PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountOut); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转出失败"); } pst.close(); } // 向accountIn账户转入money public void accountIn( String accountIn, double money) throws SQLException, AccountException { String sql = "update account set money=money+? where name=?"; Connection con=JdbcUtils.getConnection(); PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, money); pst.setString(2, accountIn); int row = pst.executeUpdate(); if (row == 0) { throw new AccountException("转入失败"); } pst.close(); } }AccountException.java (自定义异常)
package cn.itcast.exception; public class AccountException extends Exception { public AccountException() { super(); // TODO Auto-generated constructor stub } public AccountException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public AccountException(String message) { super(message); // TODO Auto-generated constructor stub } public AccountException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }JdbcUtils.java (封装Connection连接对象)
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ResourceBundle; public class JdbcUtils { private static final String DRIVERCLASS; private static final String URL; private static final String USERNAME; private static final String PASSWORD; private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static { DRIVERCLASS = ResourceBundle.getBundle("jdbc").getString("driverClass"); URL = ResourceBundle.getBundle("jdbc").getString("url"); USERNAME = ResourceBundle.getBundle("jdbc").getString("username"); PASSWORD = ResourceBundle.getBundle("jdbc").getString("password"); } static { try { // 将加载驱动操作,放置在静态代码块中.这样就保证了只加载一次. Class.forName(DRIVERCLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { Connection con = tl.get();// 从ThreadLocal中获取Connection。第一次获取得到的是null. if (con == null) { // 2.获取连接 con = DriverManager.getConnection(URL, USERNAME, PASSWORD); tl.set(con); // 将con装入到ThreadLocal中。 } // tl.remove(); //解除 return con; } // 关闭操作 public static void closeConnection(Connection con) throws SQLException { if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } } }AccountServlet.java
package cn.itcast.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.exception.AccountException; import cn.itcast.service.AccountService; public class AccountServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 1.得到请求参数 String accountIn = request.getParameter("accountin"); String accountOut = request.getParameter("accountout"); double money = Double.parseDouble(request.getParameter("money")); // 2.调用service,完成汇款操作 AccountService service = new AccountService(); try { service.account(accountIn, accountOut, money); response.getWriter().write("转账成功"); return; } catch (AccountException e) { e.printStackTrace(); response.getWriter().write(e.getMessage()); return; } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <form action="${pageContext.request.contextPath}/account" method="post"> 转入账户:<input type="text" name="accountin"><br> 转出账户:<input type="text" name="accountout"><br> 金额:<input type="text" name="money"><br> <input type="submit" value="提交"> </form> </body> </html>看运行结果:
package cn.itcast.zq; //使用继承增强 public class Demo1 { public static void main(String[] args) { Person1 p=new Student1(); p.eat(); } } class Person1{ public void eat(){ System.out.println("吃两个馒头"); } } class Student1 extends Person1{ @Override public void eat(){ super.eat(); System.out.println("加两个鸡腿"); } }
package cn.itcast.zq; //使用装饰进行增强 public class Demo2 { public static void main(String[] args) { Car car=new Bmw(); //给车增强 CarDerector cd=new CarDerector(car); cd.run(); } } interface Car { void run(); } class Bmw implements Car { public void run() { System.out.println("bmw run...."); } } class Benz implements Car { public void run() { System.out.println("benz run...."); } } // 使用装饰来完成 class CarDerector implements Car { private Car car; public CarDerector(Car car) { this.car = car; } public void run() { System.out.println("添加导航"); car.run(); } }
package cn.itcast.zq; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import cn.itcast.utils.JdbcUtils; public class Demo3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); //注册驱动 final Connection con = DriverManager.getConnection("jdbc:mysql:///mydb1","root","root"); Connection proxy = (Connection) Proxy.newProxyInstance(con.getClass() .getClassLoader(), con.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(con, args); } }); System.out.println(proxy); } }运行发现出现了如下错误:
package cn.itcast.zq; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import cn.itcast.utils.JdbcUtils; public class Demo3 { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); //注册驱动 final Connection con = DriverManager.getConnection("jdbc:mysql:///mydb1","root","root"); Connection proxy = (Connection) Proxy.newProxyInstance(con.getClass() .getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(con, args); } }); System.out.println(proxy); } }
package cn.itcast.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.LinkedList; import java.util.logging.Logger; import javax.sql.DataSource; import cn.itcast.utils.JdbcUtils; public class MyDataSource implements DataSource { private LinkedList<Connection> ll; // 用于装Connection对象的容器。 public MyDataSource() throws SQLException { ll = new LinkedList<Connection>(); // 当创建MyDateSource对象时,会向ll中装入5个Connection对象。 for (int i = 0; i < 5; i++) { Connection con = JdbcUtils.getConnection(); ll.add(con); } } // 获取连接方法 public Connection getConnection() throws SQLException { if (ll.isEmpty()) { for (int i = 0; i < 3; i++) { Connection con = JdbcUtils.getConnection(); ll.add(con); } } final Connection con = ll.removeFirst(); Connection proxyCon = (Connection) Proxy.newProxyInstance(con .getClass().getClassLoader(), new Class[]{ Connection.class}, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("close".equals(method.getName())) { // 这代表是close方法,它要做的事情是将con对象重新装入到集合中. ll.add(con); System.out.println("重新将连接对象装入到集合中"); return null; } else { return method.invoke(con, args);// 其它方法执行原来操作 } } }); return proxyCon; } // 将Connection对象重新装入. // public void readd(Connection con) { // // ll.addLast(con); // // } // public int getSize() { // return ll.size(); // } public Connection getConnection(String username, String password) throws SQLException { return null; } public PrintWriter getLogWriter() throws SQLException { return null; } public void setLogWriter(PrintWriter out) throws SQLException { } public void setLoginTimeout(int seconds) throws SQLException { } public int getLoginTimeout() throws SQLException { return 0; } public <T> T unwrap(Class<T> iface) throws SQLException { return null; } public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } }新建测试类,测试刚才新建的数据库连接池
package cn.itcast.datasource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; //测试自己定义的连接池 public class MyDataSourceTest { public static void main(String[] args) throws SQLException { DataSource mds = new MyDataSource(); // 创建一个连接池 // 获取一个连接对象 Connection con = mds.getConnection(); // 操作 ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); // 将连接对象重新装入到连接池 // mds.readd(con); con.close(); // 原来作用是将Connection对象销毁,我们在使用连接池,获取Connection对象后,在调用close方法,就不在是销毁,而是将其重新放回到连接池。 // System.out.println(mds.getSize()); } }
package cn.itcast.datasource; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.junit.Test; public class DbcpTest { // 1.手动配置 @Test public void test1() throws SQLException { BasicDataSource bds = new BasicDataSource(); // 需要设置连接数据库最基本四个条件 bds.setDriverClassName("com.mysql.jdbc.Driver"); bds.setUrl("jdbc:mysql:///mydb1"); bds.setUsername("root"); bds.setPassword("root"); // 得到一个Connection Connection con = bds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } // 2.自动配置 @Test public void test2() throws Exception { Properties props = new Properties(); // props.setProperty("driverClassName", "com.mysql.jdbc.Driver"); // props.setProperty("url", "jdbc:mysql:///day18"); // props.setProperty("username", "root"); // props.setProperty("password", "abc"); InputStream fis = this.getClass().getResourceAsStream("/dbcp.properties"); props.load(fis); DataSource ds = BasicDataSourceFactory.createDataSource(props); // 得到一个Connection Connection con = ds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } }
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///mydb1</property> <property name="user">root</property> <property name="password">root</property> </default-config> </c3p0-config>测试类如下:
package cn.itcast.datasource; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; //c3p0连接池 public class C3p0Test { // @Test public void test1() throws PropertyVetoException, SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass("com.mysql.jdbc.Driver"); cpds.setJdbcUrl("jdbc:mysql:///mydb1"); cpds.setUser("root"); cpds.setPassword("root"); // 得到一个Connection Connection con = cpds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. } @Test public void test2() throws PropertyVetoException, SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource(); // 得到一个Connection Connection con = cpds.getConnection(); ResultSet rs = con.createStatement().executeQuery( "select * from account"); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); } rs.close(); con.close(); // 将Connection对象重新装入到连接池. // String path = this.getClass().getResource("/").getPath(); // System.out.println(path); } }
<c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement">3</property> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts">30</property> <!--两次连接中间隔时间,单位毫秒。Default: 1000 --> <property name="acquireRetryDelay">1000</property> <!--连接关闭时默认将所有未提交的操作回滚。Default: false --> <property name="autoCommitOnClose">false</property> <!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试 使用。Default: null--> <property name="automaticTestTable">Test</property> <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 获取连接失败后该数据源将申明已断开并永久关闭。Default: false--> <property name="breakAfterAcquireFailure">false</property> <!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0 --> <property name="checkoutTimeout">100</property> <!--通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。 Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> <property name="connectionTesterClassName"></property> <!--指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可 Default: null--> <property name="factoryClassLocation">null</property> <!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. (文档原文)作者强烈建议不使用的一个属性--> <property name="forceIgnoreUnresolvedTransactions">false</property> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod">60</property> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize">3</property> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime">60</property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize">15</property> <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0--> <property name="maxStatements">100</property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection"></property> <!--c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过多线程实现多个操作同时被执行。Default: 3--> <property name="numHelperThreads">3</property> <!--当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0 的数据源时。Default: null--> <property name="overrideDefaultUser">root</property> <!--与overrideDefaultUser参数对应使用的一个参数。Default: null--> <property name="overrideDefaultPassword">password</property> <!--密码。Default: null--> <property name="password"></property> <!--定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 测试的表必须在初始数据源的时候就存在。Default: null--> <property name="preferredTestQuery">select id from test where id=1</property> <!--用户修改系统配置参数执行前最多等待300秒。Default: 300 --> <property name="propertyCycle">300</property> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout">false</property> <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false --> <property name="testConnectionOnCheckin">true</property> <!--用户名。Default: null--> <property name="user">root</property> <!--早期的c3p0版本对JDBC接口采用动态反射代理。在早期版本用途广泛的情况下这个参数 允许用户恢复到动态反射代理以解决不稳定的故障。最新的非反射代理更快并且已经开始 广泛的被使用,所以这个参数未必有用。现在原先的动态反射与新的非反射代理同时受到 支持,但今后可能的版本可能不支持动态反射代理。Default: false--> <property name="usesTraditionalReflectiveProxies">false</property> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">25</property> <property name="minPoolSize">10</property> <property name="maxStatements">0</property> <user-overrides user="swaldman"> </user-overrides> </default-config> <named-config name="dumbTestConfig"> <property name="maxStatements">200</property> <user-overrides user="poop"> <property name="maxStatements">300</property> </user-overrides> </named-config> </c3p0-config>
JAVAWEB开发之事务详解(mysql与JDBC下使用方法、事务的特性、锁机制)和连接池的详细使用(dbcp以d3p0)
标签:接口 last 最简 wal static cas 新建 equal user
原文地址:http://blog.csdn.net/u013087513/article/details/55210238