顯示具有 Database 標籤的文章。 顯示所有文章
顯示具有 Database 標籤的文章。 顯示所有文章

2014年1月7日 星期二

[J2EE] 2PC

There are two types of datasources suppported by JBoss

1. xa-datasource
  • XADataSource is a feature of the JDBC 2.0 Optional Package (formerly Standard Extension API).
  • If you're clustering your application servers or wanting to use distributed transactions among multiple application servers, then you should use
  • XA gets involved when you want to work with multiple resources, ex:
    • 2 or more databases
    • a database and a JMS connection
    • all of those plus maybe a JCA resource - all in a single transaction. (?)
2. tx-datasource
  • Most stuff in the world is non-XA, ex:
    • a Servlet or EJB or plain old JDBC in a Java application talking to a single database. 

Two Phase Commit (2PC)
  • 分散式資料庫為保持各個資料庫數據一致的協定。
    • 對於分散式資料庫系統來說,因為資料庫的資料是分散儲存在各地的資料庫伺服器,當交易需要執行交付時,如果馬上執行,若任何一個資料庫伺服器沒有完成交付,就有可能造成整個分散式資料庫的資料不一致。
  • 也支援 individual resource。
  • 概念為一個失敗則全部失敗。
    • 將交付分為兩個階段來執行。

Phases

1. 協調者(Coordinator)送出SECURE訊息給各資料庫伺服器,此時各資料庫伺服器交付分散儲存各處的資料庫伺服器可以執行交付,否則將放棄此交易。


2. 當各資料庫伺服器都回覆可執行交付後,協調者才送出交付訊息,讓各資料庫伺服器執行交付交易,並且回覆是否成功,如果有任何一個資料庫伺服器回覆錯誤,協調者就會送出復原訊息取消交易。


* Reference
- DataSource configuration
- 二階段交付

2012年8月25日 星期六

[ORM] Date and Timestamp

DB 中時間的 type 有:
  • Date
  • Timestamp

對應到 java 中的 type 皆是宣告為 Date

DB 都是收到相同的值 然後再依欄位 type 決定存入的值

例如若是 date 會被 truncate 掉 millisecond 後再存入

2012年7月1日 星期日

[Spring] Mapping among jsp, controller and DB

// === @ModelAttribute ===
@ReuestMapping(value=...)
public String m(
    @ModelAttribute(ATTRIBUTE_MODEL) MyObject myObject) {
    
    // 若 MyObject 前有宣告 @ModelAttribute(ATTRIBUTE_MODEL)
    // 會受 prepare() 中對 MyObject o 操作的影響
    // 若沒有宣告 則仍單純是 form bean

}

@ModelAttribute(ATTRIBUTE_MODEL)
public Object prepare() {
    // 進入 m() 前會先經過這裡
    MyObject o = new MyObject();
    return o;
}


// === URL Mapping ===
@RequestMapping(value = URL_MAPPING, method = RequestMethod.POST)
public String m(
    @RequestParam(value = ATTRIBUTE_ID, required = true) BigDecimal id) {
    // 除了 RequestMapping 中的 value 要 mapping 到以外
    // 若 RequestParam 中 required = true,request 中也必須帶有此 attribute
}


// === Mapping enum object to DB ===
// DB 內儲存的是 NAME1
// @Enumerated 便會儲存 MyObject 所對應的 name
@Enumerated (EnumType.STRING)
public MyObject getMyObject() {}

enum MyObject {
    NAME1("value1");
}


// DB 內儲存的是 DB_VALUE
// 從 DB 取出後 可藉 valueOfCode 取得相對應 MyObject
enum MyObject {
    NAME1("DB_VALUE");

    private static volatile Map<String, MyObject> registry = new HashMap<String, MyObject>();

    // Use DB_VALUE be the key of Map
    // put value to Map and use key to get MyObject
    public static MyObject valueOfCode (String code) {
        if(0 == registry.size())  {
             synchronized ( registry) {
                 if(0 == registry.size())  {
                     for(MyObject o: MyObject.values())  {
                         registry.put(o. code, o);
                     }
                 }
              }
        }
            return registry.get(code);
        }
    }

2012年6月30日 星期六

[Spring] Spring Expression Language (SpEL)

Assume get this value from DB #{#root["valueB"]}/path
// Using Expression parser, valueB should be found from DB, too.
Expression expression = parser.parseExpression(property.getValue(), 
    new TemplateParserContext());
property.setValue(expression.getValue(context, String.class));


* Reference
- Spring Expression Language (SpEL)

2012年6月15日 星期五

[Database] BigDecimal

當 table 對應為物件時

若其 value 長度可能會超過 java int

這時候就會使用 BigDecimal type

ex: 流水號


* Reference
- How big is BigDecimal?

2012年6月3日 星期日

[Database] DataSource

有下列管理 transaction 的方式:
  • JDBC
  • JTA
    • 管理的範圍較廣,可以包含多種不同來源。
  • DataSource

連至 DB 的方式:
  • Local -> server -> DB
    • 要透過 jndi 向 server 取得 look up service -> DATASOURCE。
    • Too dependent on server.
  • Local -> DB
    • Security issue(user, password).

If web service is on Server:
  • 可以直接對應 jndi 取得 Datasource。


If NOT:
  • 方式一: 利用 InitialContext預設會使用 jndi.properties 中的 jndi name。
public class DataSourceFactory {

    private static DataSource dataSource;

    public static synchronized DataSource getDataSource()
            throws ConnectionException, NamingException {
        if (dataSource != null) {
            return dataSource;
        }

        String datasourceJndiName = Configuration.getInstance().getJndiName();
        if (datasourceJndiName == null) {
            throw new ConnectionException("cann't find datasource jndi name!");
        }
        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + datasourceJndiName);
        dataSource = ds;
        return dataSource;
    }
}
  • 方式二: springframework configuration, 透過 org.springframework.jndi.JndiObjectFactoryBean。
    • 此設定是 spring 包裝 方式一 取得 datasource 的過程。
    • 在這裡可以自行指定 jndi 變數來源。
    • 若是沒有此設定,則如 方式一 預設使用 jndi.properties 中的設定,因此若沒有在此檔中提供此資訊會掛掉。

2012年1月29日 星期日

[Oracle] synonym and trigger

synonym (同義字)

  • A user 設定 x table 給 B user 能 select。



trigger

  • ex: insert 資料前要把這兩個欄位的值強制轉大寫

2011年9月23日 星期五

[JPA] javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode

Error Message
nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)


Solution

weblogic  /bea/setDomainEnv.sh 未指定 JPA jar path


if [ "${SERVER_NAME}" = "managed01d-1" ] ; then
    PRE_CLASSPATH="${DOMAIN_HOME}/lib-ext/hibernate-jpa-2.0-api-1.0.0.Final.jar"
    export PRE_CLASSPATH
fi

2011年9月18日 星期日

[Oracle] ORA-00932: 不一致的資料類型

LOB 的資料不應被用來做為 group by 條件或是直接 select 出來

因為此 type 的資料類型太大,會影響效率

在 group by 中也被限制使用 LOB

所以若是 LOB 的屬性,可另成一個物件,

和原本物件便可視為一對一的關係

等到需要時再去取出。

LOB: one-to-one LAZY FECTCH

LAZY FETCH DO not write to toString


2011年9月13日 星期二

[Oracle] SYS_CONNECT_BY_PATH


  • SYS_CONNECT_BY_PATH is valid only in hierarchical queries.
  • Provided from oracle9i.
  • It returns the path of a column value from root to node, with column values separated by char for each row returned by CONNECT BY condition.
  • Both column and char can be any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
  • The string returned is of VARCHAR2 datatype and is in the same character set as column.


* 在 Oracle 中,rownum 會比 group by、order by更先執行,因此要用子查詢。

select xxx from (select xxx from xxx order by xxx) where rownum<=xx


* Reference
- SYS_CONNECT_BY_PATH
- Rownum搭配Order by - 馬小玲傳說- 點部落
- SYS_CONNECT_BY_PATH函数用法 ORACLE - XZC.Log - BlogJava

[Oracle] Hierarchical Queries

CONNECT BY [NOCYCLE] [condition] START WITH [condition]

(or)

START WITH [condition1] CONNECT BY [condition2]

SELECT *
FROM PERSON p
CONNECT BY  p.name = PRIOR p.ID
START WITH p.ID = 0


* CONNECT BY
  • Specifies the relationship between parent rows and child rows of the hierarchy.
  • 指定 Hierarchical Queries 中 parent rows and child rows 之間的關係,當前資料會與對應的 parent rows 做比較。
  • One expression in condition must be qualified with the PRIOR operator to refer to the parent row
  • condition 中必須有一個 PRIOR,且不能包含子查詢。
  • CONNECT_BY_ISLEAF


* START WITH
  • Specifies the root row(s) of the hierarchy.
  • 指定 Hierarchical Queries 的 root (一筆或多筆),所有滿足 condition 的會被當作是 root rows。
  • 可以省略,但若沒有指定給 root,則所有資料都會被當成 root,分別掃瞄。
  • condition 可以是一個子查詢。


* PRIOR
  • 指定 parent rows。
  • Is most commonly used when comparing column values with the equality operator.
  • ex: p.name = PRIOR p.ID,表示 id of prior row = name of current name。
  • prior 放在子節點端,表示是以 START WITH 指定的節點做為跟節點,由上往下掃瞄,可能對一個或多個分支。
  • prior 放在父節點端,表示是以 START WITH 指定的節點做為最低層子節點,從下往上掃瞄,直到根節點為只,這種情況只能得到一個分支。


* LEVEL
  • Level of root row is 1,所以其 child rows 的 LEVEL = 2。


* SQL 執行順序
  1. JOIN,無論是 JOIN ON 還是在 WHERE 中做的關連
  2. CONNECT BY
  3. 其它的WHERE條件



* Reference
- Oracle Connect By
- The Oracle PL/SQL PRIOR Operator
- Hierarchical Queries
- 【原】Oracle开发专题之:级联查询(Hierarchical Queries) - pengpenglin - BlogJava
- Oracle Handbook系列之一:结构化查询(Hierarchical Queries) - SnowToday - 博客园
- oracle树中prior的用法 - space6212的个人空间 - ITPUB个人空间 - powered by X-Space

2011年9月11日 星期日

[Database] Index

索引是提高資料查詢的最有效方法。

索引的管理成本:
  1. 存儲索引的磁碟空間
  2. 執行資料修改操作(insert、update、delete)產生的索引維護
  3. 在資料處理時會需額外的回退空間。


建索引之後,資料修改時間會延長。


* Reference
- 索引是提高資料查詢的最有效方法(轉) @ oracle園地 :: 痞客邦 PIXNET ::

[Oracle] TableSpace

* Definition
是Oracle空間管理上的邏輯單位,實體上存放資料的是Tablespace裡面的檔案(Data File);而我們所熟悉的Table就放在這 一個一個的檔案裡面。
所以TableSpace可以看成是Data File的群組,是一對多的關係。


* 通常在管理上會把使用者的資料與Oracle系統的物件以不同的Tablespace做切分 。如果一個Oracle上有多個不同的AP系統,以不同的TableSpace做切割,則可以達到管理與備份的目的。但是TableSpace的功用也不僅僅只是簡單的群組分類而已,Oracle 提供了許多功能上的參數來設定TableSpace來達到空間管理與效能增進的目的。

* 有必要提的是,TableSpace沒辦法跨資料庫,TableSpace中的Data File沒辦法跨TableSpace,Data File中的Table (Segement)可以跨 Data File,但不能跨TableSpace。
簡單的來說,一個Table裡面的資料是有可能因為Oracle的空間分配而分布在同一個TableSpace的 不同的Data File中的;因此一個Data File創出來後,是不能隨便刪除的,這將會造成嚴重的資料損毀的問題。


* SYSTEM與Non-SYSTEM TableSpace
當資料庫剛建立起來,系統會建立一個叫做SYSTEM的系統TableSpace,存放SYS、SYSTEM等User重要的系統資料(ex:資料字典與預儲程序等) 如果我們建立Oracle User時,不指定預設的TableSpace,則此User則會以SYSTEM TableSpace作為預設的TableSpace。 這將造成管理上的混亂與嚴重的效能問題,這是必須特別注意的。


* 傳統的Oracle管理概念中,傾向一個tablespace中創建多個data file,特別是在多個儲存位置時,以分散 I/O,但10g後推出了BigFile Tablespace。



* Reference
- TableSpace介紹
- Oracle Bigfile Tablespace大文件表空间 - 求道的路上 - ITPUB个人空间 - powered by X-Space

2011年9月3日 星期六

[Database] CLOB and BLOB

[CLOB]
- Character Large OBject。
- 用於儲存大量的文字資料。


[BLOB]
- Binary Large Object。
- 用於儲存大量的二進位資料。


* Reference
- Java Gossip: 將檔案存入資料庫

2011年8月31日 星期三

[Oracle] ORA-01489: 字串過長

ORA-01489

Scenario:

使用sys_connect_by_path()


Message:

Cause: java.sql.SQLException: ORA-01489: 串接而成的字串過長
; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
字串串接太長 , default is VARCHAR2 (4000bytes)




20110912
JDBC 某些 driver 會有預設值限制,產生類似的問題,而用不同版本改善,可 survey 此方向相關資訊。

[Oracle ] ORA-00600

ORA-00600


Scenario:

使用 rownum 則會出現 exception。




Message:

Caused by: java.sql.SQLException: ORA-00600: 內部錯誤代碼, 引數: [evapls1], [], [], [], [], [], [], [].....

Solution:

#ALTER SYSTEM SET "_optimizer_filter_pred_pullup"=FALSE;





2011年8月27日 星期六

[Oracle] import data

index
提高搜尋資料的速度,類似於word內的索引,若沒有index,則是從第一筆的一筆一筆比對資料來確認是否符合條件,若有設的話則當符合index條件則會直接到已符合此index條件下的資料篩選

TableSpace
像是規劃不同槽的放置空間


* 使用 oracle export/import more faster than insert data directly,因為 insert 的方式轉換資料會耗很多時間。

2011年4月21日 星期四