面朝大海,春暖花开

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  12 Posts :: 1 Stories :: 3 Comments :: 0 Trackbacks

2006年4月15日 #

马上要做新项目了,项目不大,准备用ajax来练练手,HOHO!
posted @ 2006-06-05 22:09 面朝大海 阅读(151) | 评论 (0)编辑 收藏

今天在网上闲逛,发现了一本好书,关于JAVA和TDD的,内容介绍如下:

Master Java 5.0 and TDD Together: Build More Robust, Professional Software

Master Java 5.0, object-oriented design, and Test-Driven Development (TDD) by learning them together. Agile Java weaves all three into a single coherent approach to building professional, robust software systems. Jeff Langr shows exactly how Java and TDD integrate throughout the entire development lifecycle, helping you leverage today's fastest, most efficient development techniques from the very outset.

Langr writes for every programmer, even those with little or no experience with Java, object-oriented development, or agile methods. He shows how to translate oral requirements into practical tests, and then how to use those tests to create reliable, high-performance Java code that solves real problems. Agile Java doesn't just teach the core features of the Java language: it presents coded test examples for each of them. This TDD-centered approach doesn't just lead to better code: it provides powerful feedback that will help you learn Java far more rapidly. The use of TDD as a learning mechanism is a landmark departure from conventional teaching techniques.

  • Presents an expert overview of TDD and agile programming techniques from the Java developer's perspective

  • Brings together practical best practices for Java, TDD, and OO design

  • Walks through setting up Java 5.0 and writing your first program

  • Covers all the basics, including strings, packages, and more

  • Simplifies object-oriented concepts, including classes, interfaces, polymorphism, and inheritance

  • Contains detailed chapters on exceptions and logging, math, I/O, reflection, multithreading, and Swing

  • Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to annotations

  • Shows how TDD impacts system design, and vice versa

  • Complements any agile or traditional methodology, including Extreme Programming (XP)


E文不算太难,一般都应该可以看懂,为什么好书都是老外写的呢?
我想把书传上来,但是.Text对文件的大小好象是有限制,书的名字叫
Agile Java Crafting Code with Test-Driven Development,想要的同学可以留下EMAIL

posted @ 2006-05-12 19:17 面朝大海 阅读(408) | 评论 (2)编辑 收藏

很早就听说过Castle的大名了,但是一直没有时间去研究。一方面也是自己的惰性,另一方面也关于Castle的分析文章太少了。但是不管怎么说,面对这这么优秀的项目没有一睹芳容,真是。现在好了,在博客园找到了一个关于Castle的系列文章,先收藏个连接,51在休息的空隙,还可以抽出点时间好好研究好东东,
http://www.cnblogs.com/Terrylee/archive/2006/04/28/387503.html
posted @ 2006-04-28 22:04 面朝大海 阅读(252) | 评论 (0)编辑 收藏

近来很忙,一直没写 blog 了,今天晚上有点时间,听了节 MSDN 上面的一节 webCast ,讲的就是 Proxy 模式,本就就是这节课的笔记。

学习设计模式也有段时间了,很早也见过 Proxy 模式的,记得最开始接触 Proxy 模式还是在著名的 JIVE 论坛( JIVE 中用 Proxy 来做权限控制)。今天算是一遍复习吧。

对复杂的软件系统,人们常常用的一种处理手法是在系统增加一层间接层,得到对系统灵活的、满足特殊要求的解决方案。

使用 Proxy 模式的动机:在 OO 的开发过程中,某些对象的开销很大(如: new 的开销大、某些对象因为安全的原因不能被客户直接的调用等),如果直接操作这些对象会破坏系统结构。那么,我们就用代理对象来控制对象的访问。

例子,   一个常见的 HR 系统:

using  System;

class  Employee
{
    
public   double  GetSalary()
    
{
        ..
    }

    
public   void  Report()
    
{
        .
    }

    
public   void  ApplyVacation()
    
{
        
    }

}


class  HrSys
{
    
public   void  ProcessEmployee
        (Employee employee
/* 该对象和HR系统在同一个地址空间中 */ )
    
{
        employee.Report();
        ..
        employee.ApplyVacation();
    }

}

现在要求把 Employee 做成 webService HR 系统通过 webService 来调用 Employee 对象,代码修改如下:

using  System;

interface  IEmployee
{
    
public   double  GetSalary();
    
public   void  Report();
    
public   void  ApplyVacation();
}

// 运行在internet上面的某台机器
class  Employee : IEmployee
{
    
public   double  GetSalary()
    
{
        ..
    }

    
public   void  Report()
    
{
        .
    }

    
public   void  ApplyVacation()
    
{
        
    }

}

// 运行在本地的程序中
class  EmployeeProxy : IEmployee
{
    
public   double  GetSalary()
    
{
        
// 对对象创建/访问的SOAP封装
    
     
// 发送SOAP数据
    
     
// 如果有返回值,就对SOAP分析,得到C#数据
    }

    
    
public   void  Report()
    
{
        
// 对对象创建/访问的SOAP封装
    
     
// 发送SOAP数据
    
     
// 如果有返回值,就对SOAP分析,得到C#数据
    }

    
    
public   void  ApplyVacation()
    
{
        
// 对对象创建/访问的SOAP封装
    
     
// 发送SOAP数据
    
     
// 如果有返回值,就对SOAP分析,得到C#数据
    }

}

class  HrSys
{
    
public   void  ProcessEmployee(IEmployee employeeProxy)
    
{
        employeeProxy.Report();
        ..
        employeeProxy.ApplyVacation();
    }

}

Proxy 使用的要点:

1 、“增加一层间接层”,是软件系统中常用的手段之一。

2 、具体的实现中, Proxy 有很大差别,有的是简单的“ copy-on-write ”,有的是对组件模块的抽象代理。在 JAVA 中常见的 SSH 架构模式中( struts+spring+hibernate )中,我们可以把 spring 所在的服务层看成对 hiberate 的代理。

具体的实现可以参考 .NET 中的 WebService 的实现。

Copy-on-write 技术

class  App
{
    
// 系统在内存中其实是指向同一块内存
     string  s1  =   " Hello " // 不可在修改
     string  s2  =   " Hello " // 不可在修改

    
// s1.ToUpper();  // s1不会产生任何改变

    stringBulider sb1 
=   new  stringBulider( " Hello " );
    stringBulider sb2 
=   new  stringBulider( " Hello " );
    
    sb1.replace(
" H " , " L " );  // 可以修改成功
}

sb1.replace("H","L"); 系统做的动作是, sb1 的代理对象先拷贝 ”Hello” 字符串,然后用“ L ”替换“ H ”, sb1 的代理对象重新指向新的对象。

posted @ 2006-04-27 20:33 面朝大海 阅读(291) | 评论 (0)编辑 收藏

一、           数据库事务概念

数据库事务的特征: ACID

Atomic (原子性)、 Consistency (一致性)、 Isolation (隔离性)和 Durability (持久性)。 DBMS 用日志来保证数据的原子性、一致性和持久性;用锁的机制来保证数据的隔离性。

二、           事务的边界

数据库支持 2 种事务模式:自动提交和手动提交。

JDBC API 的事务边界

try
{
    Connection conn 
=  java.sql.DriverManager,.getConnection(dbUrl,dbUser,dbPwd);
    conn.setAutoCommit(
false );  // 设置成手动模式
    stmt  =  conn.createStatement();
    stmt.executeUpdate(
" . " );  // 数据库更新1
    stmt.executeUpdate( " . " );  // 数据库更新2
    
    conn.commit();
}

catch (Exception e)
{
    conn.rollback();
}

finally
{
    stmt.close();
    conn.close();
}

Hibernate API 声明事务边界

Session session  =  factory.openSession();
Transaction tx;
try  
{
    tx 
=  session.beginTransaction();  // 开始事务
    
// 执行操作
    。。。。。
    
    tx.commit();
}

catch  (Exception e)
{
    
if  (tx != null )
    
{
        tx.rollback();
    }

}

finally
{
    session.close();
}

注:一个 session 可以对应多个事务,但是推荐的做法是一个 session 对应一个事务。

三、            多事务的并发问题

当多个事务同时访问相同的数据的时候,程序如果没有采取适当的隔离措施,就会发生数据库的并发问题。常见的并发问题有:

第一类丢失更新:撤消事务的时候,把其他的事务已经提交的数据给覆盖了;

脏读;读了没有提交的数据;

虚读:一个事务读到另外一个事务已经提交的新插入的数据;

不可重复读:一个事务读到另外一个事务已经提交的更新的数据;

第二类丢失更新:一个事务覆盖另外一个事务已经提交的更新数据。 
四、          

一般地,大型的 DBMS 都会自动的管理锁定机制,但是在对数据的安全性、完整性和一致性有特殊要求的地方,可以由事务本身来管理琐的机制。

有一点要关注的是:锁的粒度越大,隔离性越好,并发性越差。

按照锁的程度来分有:

共享锁:用读操作,非独占的,其他事务可以读,但是不能更新,并发性好;

独占锁:用与 insert update delete 等语句,其他事务不能读,也不能改,并发性差;

更新锁:执行 update 的时候,加锁。

死琐:多是事务分别锁定了一个资源,又请求锁定对方已经锁定的资源,就造成了请求环。

降低死锁的最好办法是使用短事务。

五、            数据库的事务隔离级别

数据库提供 4 种事务隔离级别:

Serializable :串行化;(隔离级别最高) 1

Repeatable Read :可重复读; 2

Read Commited :读已提交数据; 4

Read Uncommited :读未提交数据;(隔离级别最低) 8

Hiberate 中的隔离级别的设置

Hibernate 的配置文件中 hibernate.connection.isolation=2

六、            悲观锁和乐观琐

从应用程序的角度来看,锁分为悲观锁和乐观锁。

悲观锁:显示的为程序加锁,但是降低并发性。

Select ……. For update;

Hibernate 中的代码

Session.get(Account.class,net Long(1),LockMode.UPGRADE) // 程序采用悲观锁

乐观锁:依靠 DBMS 来管理锁,程序依靠版本控制来避免并发问题。

在对象 - 关系映射的文件中,用 <version> 或者 <timestamp> 可以管理并发。乐观琐比悲观琐有更好的并发性,优先考虑乐观琐。  

posted @ 2006-04-22 17:12 面朝大海 阅读(866) | 评论 (0)编辑 收藏

     摘要: 1. 你们的项目组使用源代码管理工具了么?     应该用。 VSS 、 CVS 、 PVCS 、 ClearCase 、 CCC/Harvest 、 FireFly 都可以。我的选择是 VSS 。 ...  阅读全文
posted @ 2006-04-15 20:38 面朝大海 阅读(175) | 评论 (0)编辑 收藏