1 package net.sf.sapjcosupport;
2
3 import com.sap.mw.jco.IRepository;
4 import com.sap.mw.jco.JCO;
5 import org.apache.log4j.Logger;
6 import org.springframework.dao.DataAccessResourceFailureException;
7
8 /**
9 * @author Niki Driessen
10 * @since Jan 17, 2006 - 12:53:29 PM
11 */
12 public class SapDataSource {
13 private static final Logger logger = Logger.getLogger(SapDataSource.class);
14
15 private static JCO.Pool pool;
16 private static IRepository repository;
17
18 private String username;
19 private String password;
20 private String host;
21 private String client;
22 private String systemNr;
23 private String language = "EN";
24 private int poolSize = 5;
25
26 public String getUsername() {
27 return username;
28 }
29
30 public void setUsername(String username) {
31 this.username = username;
32 }
33
34 public String getPassword() {
35 return password;
36 }
37
38 public void setPassword(String password) {
39 this.password = password;
40 }
41
42 public String getHost() {
43 return host;
44 }
45
46 public void setHost(String host) {
47 this.host = host;
48 }
49
50 public String getClient() {
51 return client;
52 }
53
54 public void setClient(String client) {
55 this.client = client;
56 }
57
58 public String getSystemNr() {
59 return systemNr;
60 }
61
62 public void setSystemNr(String systemNr) {
63 this.systemNr = systemNr;
64 }
65
66 public String getLanguage() {
67 return language;
68 }
69
70 public void setLanguage(String language) {
71 this.language = language;
72 }
73
74 public int getPoolSize() {
75 return poolSize;
76 }
77
78 public void setPoolSize(int poolSize) {
79 this.poolSize = poolSize;
80 }
81
82 public IRepository getRepository() {
83 getConnectionPool();
84 return repository;
85 }
86
87 public synchronized JCO.Client getConnection() throws DataAccessResourceFailureException {
88 try {
89 JCO.Pool pool = getConnectionPool();
90 return JCO.getClient(pool.getName());
91 } catch (Exception e) {
92 throw new DataAccessResourceFailureException("Error creating connection to SAP", e);
93 }
94 }
95
96 private synchronized JCO.Pool getConnectionPool() {
97 if (pool == null) {
98 String poolName = new StringBuffer("pool_").append(host).append(systemNr).append(client).toString();
99 pool = JCO.getClientPoolManager().getPool(poolName);
100 if (pool == null) {
101 logger.info(new StringBuffer("Creating new pool ").append(poolName).append(" , max size: ").append(
102 poolSize).toString());
103 JCO.addClientPool(poolName, poolSize,
104 client, username, password, language, host, systemNr);
105 pool = JCO.getClientPoolManager().getPool(poolName);
106 pool.setConnectionTimeout(60000);
107 pool.setMaxWaitTime(60000);
108 pool.setMaxConnections(poolSize * 2);
109 }
110
111 if (repository == null) {
112 repository = JCO.createRepository("DefaultRepository", poolName);
113 }
114 }
115 return pool;
116 }
117
118 public synchronized void release(JCO.Client connection) {
119 if (connection != null) {
120 try {
121 JCO.releaseClient(connection);
122 } catch (Exception e) {
123 logger.warn("Error during release of connection...Trying to reset connection", e);
124 try {
125 connection.reset();
126 JCO.releaseClient(connection);
127 } catch (Exception e1) {
128
129 }
130 }
131 }
132 }
133 }